代码之家  ›  专栏  ›  技术社区  ›  Vineel Kumar Reddy

C的单元测试框架[关闭]

c
  •  15
  • Vineel Kumar Reddy  · 技术社区  · 16 年前

    对于Java和.NET,是否有类似于JUnit和NUnit之类的C单元测试框架? 或者,我们如何测试用C编写的代码,以适应不同的场景?

    提前谢谢……

    7 回复  |  直到 10 年前
        1
  •  6
  •   tilljoel    16 年前

    我和一些人一起工作过 Check 而且很容易设置。它被一些大型的活动项目使用,比如 GStreamer . 下面是一个简单的失败的例子,如果行:

    fail_if (0 == get_element_position(spot), "Position should not be 0");
    
        3
  •  2
  •   Keith Nicholas    15 年前

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

    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;
    }
    
        4
  •  2
  •   flbulgarelli    10 年前

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

    如果你曾经用过 RSPEC 或 摩卡 使用起来很简单。它甚至不需要你写一个 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
    
    }
    
        5
  •  1
  •   Community Mohan Dere    9 年前

    我还不熟悉单元测试框架,但最近我尝试过 cut , check 和 cunit . 这似乎与其他人的经历相反(见 Unit Testing C Code 对于之前的问题),但我觉得最容易去的是城市大学。这对我来说也是一个不错的选择,因为CUnit应该与其他XUnit框架保持良好的一致性,并且我经常切换语言。

        6
  •  1
  •   David Wolever    16 年前

    我很高兴 CuTest 上次我需要对测试C进行单元测试时,它只有一个.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"));
    }   
    
        7
  •  0
  •   David Holm    11 年前

    好吧,用C替换J,从…C

    Cunit

    尽管可能 CUT 更通用。

    最后,我可能会因为我真的喜欢NetBSD而感到不安,但你也应该 ATF 尝试