如何编写多线程单元测试? [英] How to write multi-threaded unit tests?

查看:300
本文介绍了如何编写多线程单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一些单元测试框架能够轻松编写多线程测试?

I'd like to know if there are some unit testing frameworks which are capable of writing multi-threaded tests easily?

我会想到类似的东西: 同时由n个线程调用特殊的测试方法m次.在所有测试线程完成后,将调用一个应验证某些约束的断言方法.

I would imagine something like: invoke a special test method by n threads at the same time for m times. After all test threads finished, an assertion method where some constraints should be validated would be invoked.

我当前的方法是在junit测试方法内创建Thread对象,在每个run()方法内手动循环真实的测试用例,等待所有线程,然后验证断言.但是使用这个,我为每个测试都有一个大的样板代码块.

My current approach is to create Thread objects inside a junit test method, loop manually the real test cases inside each run() method, wait for all threads and then validate the assertions. But using this, I have a large boilerplate code block for each test.

您的经历是什么?

推荐答案

GroboUtils .

很多年前,我已经使用过GroboUtils,它确实完成了工作. ConTest是较新的,并且现在将是我的首选起点,因为该工具不仅依赖于反复试验,还强制执行线程的特定交织,从而提供确定性的测试.相比之下,GroboUtils MultiThreadedTestRunner只是运行测试,并希望调度程序产生一种交错现象,从而导致线程错误的出现.

I've used GroboUtils many years ago, and it did the job. ConTest is newer, and would be my preferred starting point now, since rather than just relying on trial and error, the instrumentation forces specific interleavings of the threads, providing a deterministic test. In contrast, GroboUtils MultiThreadedTestRunner simply runs the tests and hopes the scheduler produces an interleaving that causes the thread bug to appear.

另请参见 ConcuTest ,它也可以强制插入并且是免费的

See also ConcuTest which also forces interleavings and is free.

这篇关于如何编写多线程单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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