Cunit testing框架

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

提前致谢……

我已经使用Check进行了一些操作并且非常容易设置。 它被一些像GStreamer这样的大型活跃项目所使用。 以下是一个简单的失败示例:

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

Glib内置测试框架: http : //library.gnome.org/devel/glib/stable/glib-Testing.html

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

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; } 

还有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 } 

我还是unit testing框架的新手,但我最近尝试过cut , check和cunit 。 这似乎与其他人的经历相反(参见上一个问题的unit testingC代码 ),但我发现cunit是最容易开始的。 这对我来说似乎也是一个不错的选择,因为cunit应该与其他xunit框架很好地配合,并且我会频繁地切换语言。

我最后一次需要unit testingC时,我对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")); } 

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

库尼特

虽然可能CUT更通用。

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