C的单元测试框架

时间:2010-04-30 15:44:18

标签: c

是否有适用于Java和.NET的JUnit和Nunit等C的单元测试框架? 或者我们如何针对不同的场景测试用C编写的一段代码?

提前致谢......

7 个答案:

答案 0 :(得分:7)

我曾与Check合作过,而且很容易设置。它被GStreamer等一些大型活跃项目所使用。这是一个简单的失败示例:行:

fail_if (0 == get_element_position(spot), "Position should not be 0");

答案 1 :(得分:3)

答案 2 :(得分:2)

最重要的是我为自己写的并且是开源的。目标是简单,并有一个干净的语法。

http://code.google.com/p/seatest/

一种基本的简单测试就是......

#include "seatest.h"
//
// create a test...
//
void test_hello_world()
{
    char *s = "hello world!";
    assert_string_equal("hello world!", s);
    assert_string_contains("hello", s);
    assert_string_doesnt_contain("goodbye", s);
    assert_string_ends_with("!", s);
    assert_string_starts_with("hell", s);
}

//
// put the test into a fixture...
//
void test_fixture_hello( void )
{
    test_fixture_start();      
    run_test(test_hello_world);   
    test_fixture_end();       
}

//
// put the fixture into a suite...
//
void all_tests( void )
{
    test_fixture_hello();   
}

//
// run the suite!
//
int main( int argc, char** argv )
{
    run_tests(all_tests);   
    return 0;
}

答案 3 :(得分:2)

还有cspec,这是一个非常简单易用的BDD框架。

如果你曾经使用过诸如 rspec mocha 这样的东西,那么使用它将是微不足道的。它甚至不需要你编写main函数。

以下是一个例子:

context (example) {

    describe("Hello world") {

        it("true should be true") {
            should_bool(true) be equal to(true);
        } end

        it("true shouldn't be false") {
            should_bool(true) not be equal to(false);
        } end

        it("this test will fail because 10 is not equal to 11") {
            should_int(10) be equal to(11);
        } end

        skip("this test will fail because \"Hello\" is not \"Bye\"") {
            should_string("Hello") be equal to("Bye");
        } end

    } end

}

答案 4 :(得分:1)

我还是单元测试框架的新手,但我最近尝试过cutcheckcunit。这似乎与其他人的经历相反(前一个问题请参阅Unit Testing C Code),但我发现cunit最容易开始。这对我来说似乎也是一个不错的选择,因为cunit应该与其他xunit框架很好地配合,并且我会频繁地切换语言。

答案 5 :(得分:1)

我最后一次需要单元测试时对CuTest非常满意。它只有一个.c / .h对,带有一个小的shell脚本,可自动找到构建测试套件的所有测试并且断言错误并非完全无益。

以下是我的一项测试示例:

void TestBadPaths(CuTest *tc) {
    // Directory doesn't exist
    char *path = (char *)"/foo/bar";
    CuAssertPtrEquals(tc, NULL, searchpath(path, "sh"));

    // A binary which isn't found
    path = (char *)"/bin";
    CuAssertPtrEquals(tc, NULL, searchpath(path, "foobar"));
}   

答案 6 :(得分:0)

好吧,只需用C替换Java中的J ......

Cunit

虽然可能CUT更通用。

最后我可能会因为我非常喜欢NetBSD而感到厌烦,但你也应该尝试ATF

相关问题