TestNG 并行测试失败
首先让我列出测试的结构:
- BaseTest(Singleton) 包含一个运行
@BeforeSuite
的setUp()
方法。此 setUp() 方法初始化 MyObject,其声明为:
protected static ThreadLocal
所有其他测试都扩展此BaseTest。 例如说CustomerTest
这个客户测试有 -
- 使用
@BeforeClass
标记进行测试 - 它获取 MyObject 的存储实例。 - 其他测试将使用 MyObject,执行一些操作并进行测试
- 使用
@AfterClass
标记进行的测试 - 确实会破坏 MyObject 的实例
- 使用
因此理想情况下,此 setUp() 方法应在任何其他测试之前运行。并且它仅运行一次。
我正在尝试在 TestNG 框架中并行运行测试用例。为此,我将 testng.xml 的 suite
标记上的并行属性设置为
后的几秒钟内,构建失败,所有基本测试都失败,其他测试被跳过。
失败的测试是由于 java.lang.NullPointerException
我的理解是,当 setUp() 方法在线程上运行时,不同线程上的其他一些测试正在尝试访问未初始化的 MyObject然而。失败也是如此。我的理解正确吗?
如果是,可能的解决方案是什么?
我可以做类似的事情 - 让正在运行 setUp() 的线程先运行,然后不要让其他线程调用。一旦对 setUp() 的调用完成/返回,就允许其他线程调用。
(注意:我的项目使用Maven)
First let me put out the structure of my tests:
- There is BaseTest(Singleton) containing a
setUp()
method which runs@BeforeSuite
. This setUp() method initializes MyObject which is declared as:
protected static ThreadLocal<MyObject> myObject= new ThreadLocal<MyObject>();
All other tests extend this BaseTest.
e.g. Say CustomerTestThis CustomerTest have -
- A test with
@BeforeClass
tag - it gets the stored instance of MyObject. - Other tests will use MyObject, perform some operation and do the tests
- A test with
@AfterClass
tag - does destroy the instance of MyObject
- A test with
So ideally, this setUp() method should run before any other test. And it runs only once.
I am trying to run test cases parallely in TestNG framework. To achieve that, I have set the parallel attribute on the suite
tag of testng.xml as
<suite name="Suite" parallel="classes" thread-count="5">
Now, within the seconds after I fire the build, the build gets failed with all basic tests failed and others as skipped.
Failed test are due to java.lang.NullPointerException
My understanding is, while setUp() method is being run on a thread, some other tests on different threads are trying to access the MyObject which isn't initialized yet. So is the failure. Is my understanding correct?
If yes, what could be the possible solution to this?
Can I do something like - let the thread run first in which setUp() is being run and till then don't let other threads invoke. And once the call to setUp() finishes/ returns, then allow other threads to invoke.
(Note: My project uses Maven)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有两件事:
NullPointerExceptions 非常简单
追踪,那是什么
告诉你?您应该包括
堆栈跟踪和代码。
@BeforeSuite 只会运行一次,
但根据你所说的,你
期望它在每次测试之前运行
方法,这意味着你应该使用
@BeforeMethod.
Two things:
NullPointerExceptions are pretty easy
to track down, what does that one
tell you? You should include the
stack trace and the code.
@BeforeSuite will only be run once,
but based on what you're saying, you
expect it to run before each test
method, which means you should use
@BeforeMethod.
我以前遇到过这个问题。我能够解决这个问题的唯一方法是删除设置方法上的
BeforeSuite
标签。然后每个类BeforeClass
都会询问是否已初始化,如果是,则继续,否则,将调用setup。当然,您还必须将所有这些方法同步到同一个对象。这是我发现解决这个问题的唯一方法。
I have encountered this problem before. The only way I was able to overcome the issue was to remove the
BeforeSuite
tag on the setup method. Then each classesBeforeClass
would ask if it is initialized, and if so, keep going, otherwise, it would call setup. Of course you would also have tosynchronize
all of these methods to the same object.That is the only way I have found to solve this problem.