【问题标题】:unit testing framework for C [closed]C的单元测试框架[关闭]
【发布时间】:2011-02-14 07:17:40
【问题描述】:

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

提前致谢......

【问题讨论】:

标签: c


【解决方案1】:

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

如果您曾经使用过 rspecmocha 之类的东西,使用起来会很简单。它甚至不需要您编写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

}

【讨论】:

    【解决方案2】:

    好吧,只需将 Java 中的 J 替换为来自... C 的 C

    Cunit

    虽然CUT 可能更通用。

    最后我可能会被淘汰,因为我真的很喜欢 NetBSD,但你也应该试试ATF

    【讨论】:

      【解决方案3】:

      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】:

        我曾与Check 合作过,它很容易设置。它被一些大型活动项目使用,例如GStreamer。下面是一个失败 if 行的简单示例:

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

        【讨论】:

          【解决方案5】:

          上次我需要对 C 进行单元测试时,我对 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】:

            我还是单元测试框架的新手,但我最近尝试了cutcheckcunit。这似乎与其他一些人的经历相矛盾(有关上一个问题,请参阅 Unit Testing C Code),但我发现 cunit 最容易上手。这对我来说似乎也是一个不错的选择,因为 cunit 应该与其他 xunit 框架很好地保持一致,而且我会经常切换语言。

            【讨论】:

              【解决方案7】:

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2010-09-19
                • 1970-01-01
                • 2010-10-18
                • 2011-01-16
                • 1970-01-01
                • 2012-10-18
                • 1970-01-01
                • 2011-09-10
                相关资源
                最近更新 更多