本文介绍了如何在TestNG中实现singleThreaded = true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在TestNG中,我试图理解@Test注释的singleThreaded=true外观的使用.我已经提到了 http://testng.org/doc/documentation-main.html http://beust.com/weblog2/archives/000407.html 但没有得到了很多帮助.

In TestNG i am trying to undertand the use of singleThreaded=true attibute of @Test Annotation .I already referred http://testng.org/doc/documentation-main.html and http://beust.com/weblog2/archives/000407.html but didn't got much help .

我的问题:为什么我们需要在单线程上执行方法.在多线程上运行可以节省时间.

My Question : Why do we need to execute method on single thread. Running on multiple thread can save out time.

注意:在 http://beust.com/weblog2/archives/000407给出的示例中.html

他说::这两种测试方法testf1()testf2()分别测试方法A#f1A#f2,但是当您要求TestNG以并行模式运行这些测试时,这两种方法会从不同的线程调用,如果它们之间无法正确同步,您很可能最终会处于损坏状态.

He said :: "the two test methods testf1() and testf2() test the methods A#f1 and A#f2 respectively, but when you ask TestNG to run these tests in parallel mode, these two methods will be invoked from different threads, and if they don't properly synchronize with each other, you will most likely end up in a corrupt state.

任何人都可以用代码解释上面的示例

Can anyone explain with code the above example

推荐答案

如此处所述 http: //beust.com/weblog2/archives/000407.html ...如果要测试的类不是多线程安全的...

因此,您的测试可以并行运行,但是如果您测试的不是线程安全的类,则可能会中断.

So your tests can run in parallel, but if you are testing classes which are not thread safe things can break.

在示例中,一个测试调用a.f1(),第二个测试调用a.f2().如果这些方法使用对象this.a = new A()中的某些共享资源并且相互依赖,则很有可能最终会导致对象this.a处于损坏状态.

In example one test calls a.f1() and second one calls a.f2(). If these methods uses some shared resources in object this.a = new A() and depends on each other you will most likely end up in a corrupt state with object this.a.

这篇关于如何在TestNG中实现singleThreaded = true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 08:03