JUnit的.并行运行.但是所有测试方法都处理单例实例.怎么解决? [英] JUnit. Parallel running. But all the test methods processing singleton instance. How to solve?
问题描述
因此,我有几个JUnit
类,每个类都包含一个测试方法列表.
每种方法彼此独立,没有直接连接.
但是我们有间接连接:所有方法都处理一个单例对象(它是Selenium Web Driver
实例,是的,我对所有测试都使用1 Web Driver
实例,因为创建新的对象实例需要花费大量时间!).
So, I have a couple of JUnit
classes, each one contains a list of test methods.
Each method is independent of each other, there is no direct connection.
But we have indirect connection: all methods processes one singleton object (it is Selenium Web Driver
Instance, yes, I use 1 Web Driver
Instance for all my tests, because for making new object instance we spend a really lot of time! ).
当测试方法在一个线程中逐步执行时,这没关系.但这太长了,
And It is all ok, when test methods execute step by step in one thread. But it is too long too,
因此,我决定提高速度, 如何? -我决定以并行模式运行所有测试方法.为此,我使用具有特殊配置的maven进行并行测试执行.
So, I decided to increase speed, How? - I decided to run all the test methods in the parallel mode. For this I use maven with the special configuration for parallel test execution.
但是,我认为这是一个新问题,因为-结果我们执行了并行方法,但是我们仍然只使用单个 Web驱动程序实例.
But I think, it is a source a new problem, because - in result we have parallel methods execution, but we still work just with single Web Driver Instance.
我正在寻找最佳解决方案:
I'm trying to find the optimal solution:
我希望测试将以并行模式执行-确实非常快.
I want that the tests will be executed in parallel mode - it is really fast.
我不希望为每个测试都创建一个新对象-这是一个非常漫长的过程.
I don't want that for every test new object is created - it is a very long process.
您能为我提供什么建议?
What advice can you provide for me?
您将如何解决这个问题?
How would you have solved this problem?
推荐答案
不幸的是,webDriver不是线程安全的.恕我直言,最佳做法是使用单独的webDriver实例在单独的线程中运行每个测试类.最佳线程数是
int threadNum = Runtime.getRuntime().availableProcessors() * 2;
我的项目的执行时间从30分钟减少到4分钟.
在Thucydides框架中使用的方法完全相同.
Unfortunately, webDriver is not thread-safe. Imho, best practice is to run each test class using individual webDriver instance in separate thread. The optimal number of threads is
int threadNum = Runtime.getRuntime().availableProcessors() * 2;
The executing time of my projects reduced from 30 minutes to 4.
Exactly the same method is used in Thucydides framework.
这篇关于JUnit的.并行运行.但是所有测试方法都处理单例实例.怎么解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!