【问题标题】:Testable C application using posix threads使用 posix 线程的可测试 C 应用程序
【发布时间】: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


【解决方案1】:

我只知道在 C 中模拟函数的一种方法:拥有指向可模拟函数的函数指针,并且只能通过该指针调用它们。对于您的测试,您可以调整指针以指向您的模拟函数。但我不知道你是否真的想走那条路……

【讨论】:

    猜你喜欢
    • 2010-10-06
    • 1970-01-01
    • 2016-09-01
    • 2012-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多