并发 JUnit 测试 [英] Concurrent JUnit testing

查看:30
本文介绍了并发 JUnit 测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的 JUnit 测试套件,我很想在其中同时运行所有测试,原因有两个:

I have a large JUnit test suite, where I'd quite like to run all the tests concurrently for two reasons:

  • 利用多个内核以更快地运行整个测试套件
  • 希望能检测到由于非线程安全的全局对象导致的一些错误

我意识到这将迫使我重构一些代码以使其成为线程安全的,但我认为这是一件好事:-)

I recognise that this will force me to refactor some code to make it thread-safe, but I consider that to be a good thing :-)

让 JUnit 同时运行所有测试的最佳方法是什么?

What's the best way to get JUnit to run all the tests concurrently?

推荐答案

您是否固定使用 JUnit?TestNG 提供了良好的开箱即用多线程测试,并且它与 JUnit 测试兼容(您需要进行一些更改).例如,您可以运行这样的测试:

Are you fixed to JUnit? TestNG provides good multi thread testing out of the box and it's compatible with JUnit tests (you need to make a few changes). For example you could run a test like this:

@Test(threadPoolSize = 3, invocationCount = 9,  timeOut = 10000)
public void doSomething() {
...
}

这意味着 doSomething() 方法将被 3 个不同的线程调用 9 次.

This would mean that the doSomething() method will be invoked 9 times by 3 different threads.

我强烈推荐TestNG.

这篇关于并发 JUnit 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆