TestNG:运行 类 在并行线程中安全吗?
TestNG: Is running classes in parallel thread safe?
当我 运行 我的测试 类 并行时,失败测试的数量高于如果我 运行 测试 类 序列化。为了调试目的,我在控制台上记录了很多,但是当 运行 并行时它几乎没用,因为记录的所有内容都是 jumbled/out 的顺序。
在我尝试修复日志记录问题以便更好地调试之前,我想知道,如果我设置 parallel="classes"
,它能保证线程安全吗?我假设如果我设置 parallel="tests"
两个方法可能会尝试访问范围内的同一个对象,从而导致线程安全问题。
即:
<suite name="test" parallel="classes" thread-count="3">
来自 TestNG 文档 5.10.2 - Parallel tests, classes and methods:
The parallel attribute on the tag can take one of following values:
<suite name="My suite" parallel="methods" thread-count="5">
<suite name="My suite" parallel="tests" thread-count="5">
<suite name="My suite" parallel="classes" thread-count="5">
<suite name="My suite" parallel="instances" thread-count="5">
parallel="methods"
: TestNG will run all your test methods in separate threads. Dependent methods will also run in separate threads but they will respect the order that you specified.
parallel="tests"
: TestNG will run all the methods in the same tag in the same thread, but each tag will be in a separate thread. This allows you to group all your classes that are not thread safe in the same and guarantee they will all run in the same thread while taking advantage of TestNG using as many threads as possible to run your tests.
parallel="classes"
: TestNG will run all the methods in the same class in the same thread, but each class will be run in a separate thread.
parallel="instances"
: TestNG will run all the methods in the same instance in the same thread, but two methods on two different instances will be running in different threads.
因此,如果“线程安全”是指不会有超过一个 TestNG 线程与任何给定测试 class 实例交互,那么是的,使用“classes”将给你你想要的。然而,“测试”并不是 运行 每个测试方法都在它自己的线程中,而是每个 <test>
来自你的标签 testng.xml。以上描述应该有所帮助。
当我 运行 我的测试 类 并行时,失败测试的数量高于如果我 运行 测试 类 序列化。为了调试目的,我在控制台上记录了很多,但是当 运行 并行时它几乎没用,因为记录的所有内容都是 jumbled/out 的顺序。
在我尝试修复日志记录问题以便更好地调试之前,我想知道,如果我设置 parallel="classes"
,它能保证线程安全吗?我假设如果我设置 parallel="tests"
两个方法可能会尝试访问范围内的同一个对象,从而导致线程安全问题。
即:
<suite name="test" parallel="classes" thread-count="3">
来自 TestNG 文档 5.10.2 - Parallel tests, classes and methods:
The parallel attribute on the tag can take one of following values:
<suite name="My suite" parallel="methods" thread-count="5">
<suite name="My suite" parallel="tests" thread-count="5">
<suite name="My suite" parallel="classes" thread-count="5">
<suite name="My suite" parallel="instances" thread-count="5">
parallel="methods"
: TestNG will run all your test methods in separate threads. Dependent methods will also run in separate threads but they will respect the order that you specified.
parallel="tests"
: TestNG will run all the methods in the same tag in the same thread, but each tag will be in a separate thread. This allows you to group all your classes that are not thread safe in the same and guarantee they will all run in the same thread while taking advantage of TestNG using as many threads as possible to run your tests.
parallel="classes"
: TestNG will run all the methods in the same class in the same thread, but each class will be run in a separate thread.
parallel="instances"
: TestNG will run all the methods in the same instance in the same thread, but two methods on two different instances will be running in different threads.
因此,如果“线程安全”是指不会有超过一个 TestNG 线程与任何给定测试 class 实例交互,那么是的,使用“classes”将给你你想要的。然而,“测试”并不是 运行 每个测试方法都在它自己的线程中,而是每个 <test>
来自你的标签 testng.xml。以上描述应该有所帮助。