【发布时间】:2012-10-20 18:21:07
【问题描述】:
我必须编写看起来会做类似事情的代码(当然要复杂得多):
int stop;
int isStopped;
void workerFunction(){
while(!stop){
//...
}
isStopeed = 1;
}
startThread(){
int newThreadPid = pthread_create(..., NULL, workerFunction, NULL);
}
stopThread(){
stop = 1;
}
我的问题是:是否有任何技术可以使此类代码可测试?我不认为编写测试像:
startThread();
stopThread();
sleep(1);
ASSERT(isStopped);
是最好的主意。如何在不调用系统函数的情况下测试 stopThread() 函数?有什么方法可以模拟 pthread_create 吗?
【问题讨论】:
-
真的不清楚你在问什么。
-
我不希望单元测试调用系统函数。可以准模拟 pthread_create 吗?
标签: c unit-testing tdd pthreads testability