简体   繁体   English

TestNG并行测试失败

[英]TestNG parallel tests failing

First let me put out the structure of my tests: 首先让我介绍一下测试的结构:

  • There is BaseTest (Singleton) containing a setUp() method which runs @BeforeSuite . 有一个BaseTest (Singleton),其中包含一个运行@BeforeSuitesetUp()方法。 This setUp() method initializes MyObject which is declared as: 此setUp()方法将MyObject初始化为:

protected static ThreadLocal<MyObject> myObject= new ThreadLocal<MyObject>();

  • All other tests extend this BaseTest . 所有其他测试都扩展了此BaseTest eg Say CustomerTest 例如说客户测试

    This CustomerTest have - CustomerTest具有-

    1. A test with @BeforeClass tag - it gets the stored instance of MyObject. 带有@BeforeClass标记的测试-它获取MyObject的存储实例。
    2. Other tests will use MyObject, perform some operation and do the tests 其他测试将使用MyObject,执行一些操作并进行测试
    3. A test with @AfterClass tag - does destroy the instance of MyObject 使用@AfterClass标记的测试-确实会销毁MyObject的实例

So ideally, this setUp() method should run before any other test. 因此,理想情况下,此setUp()方法应在任何其他测试之前运行。 And it runs only once . 而且它只运行一次

I am trying to run test cases parallely in TestNG framework. 我正在尝试在TestNG框架中并行运行测试用例。 To achieve that, I have set the parallel attribute on the suite tag of testng.xml as 为此,我在testng.xml的suite标签上将parallel属性设置为

<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 测试失败是由于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. 我的理解是,在线程上运行setUp()方法时,在不同线程上进行的其他一些测试正在尝试访问尚未初始化的MyObject。 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. 我可以做些什么-让线程先在其中运行setUp()的情况下运行,然后再让其他线程不要调用。 And once the call to setUp() finishes/ returns, then allow other threads to invoke. 一旦对setUp()的调用完成/返回,则允许其他线程调用。

( Note: My project uses Maven) 注:我的项目使用Maven)

Two things: 两件事情:

  • NullPointerExceptions are pretty easy to track down, what does that one tell you? NullPointerException非常容易追踪,那告诉你什么? 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只会运行一次,但是根据您的意思,您希望它在每个测试方法之前运行,这意味着您应该使用@BeforeMethod。

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. 我能够解决此问题的唯一方法是删除设置方法上的BeforeSuite标记。 Then each classes BeforeClass would ask if it is initialized, and if so, keep going, otherwise, it would call setup. 然后,每个类BeforeClass都会询问它是否已初始化,如果已初始化,请继续进行,否则,它将调用setup。 Of course you would also have to synchronize all of these methods to the same object. 当然,您还必须将所有这些方法synchronize到同一对象。 That is the only way I have found to solve this problem. 这是我发现解决此问题的唯一方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM