多线程单元测试 [英] Multithread a unit test

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

问题描述

为了测试并发问题,我想使用单元测试同时在两个不同的线程上调用相同的方法.实际上,我可能想同时在多个线程上调用相同的方法.

In order to test concurrency issues, I want to call the same method on two different threads at exactly the same time using a unit test. In fact I probably want to call the same method on several threads at the same time.

我正在使用Microsoft内置的VS2008单元测试器.

I'm using Microsoft's built in unit tester for VS2008.

我的想法是,我将锁定一个对象,然后同步设置每个线程,这些线程将立即等待该锁,然后释放该锁,这将允许所有线程访问共享资源并测试代码的功能,正确处理多个线程.

My thoughts are that I would lock an object and then synchronously setup each of the threads which would immediately wait on the lock and then release the lock which would allow all threads to access the shared resources and test the ability of the code to correctly handle multiple threads.

有人做过吗?我在这里采取合理的方法吗?

Has anybody done this before? Am I taking a reasonable approach here?

推荐答案

我以前这样做过,即使结果并不总是可重复的,它们也会突出显示代码中的问题或错误.

I have done this before and even though the results are not always reproducible, they can highlight problems or errors in your code.

尽管我不使用Microsoft的单元测试,但我更喜欢 MbUnit ,它具有ThreadedRepeat属性,允许您指定要运行的线程数.只需编写一个测试并添加一个属性,它就可以简化整个多线程测试的过程.

I don't use Microsoft's unit testing for it though, I prefer MbUnit which has the ThreadedRepeat attribute which allows you to specify how many threads you want to run. It make the whole multi-threaded test thing much easier, just write a test and add an attribute.

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

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