C语言的单元测试框架
有没有针对C的单元测试框架,比如针对java和.NET的JUnit和Nunit? 或者,我们如何针对不同的场景测试用C编写的代码C语言的单元测试框架,c,C,有没有针对C的单元测试框架,比如针对java和.NET的JUnit和Nunit? 或者,我们如何针对不同的场景测试用C编写的代码 提前感谢……好吧,把Java中的J替换成C。。。C 虽然可能更一般 最后,我可能会被拒绝,因为我非常喜欢NetBSD,但您也应该尝试一下Glib的内置测试框架: 我对单元测试框架还是新手,但最近我尝试了,而且。这似乎与其他一些人的经历相反(参见前面的问题),但我发现cunit是最容易开始的。对我来说,这似乎也是一个不错的选择,因为cunit应该与其他xunit框架保
提前感谢……好吧,把Java中的J替换成C。。。C 虽然可能更一般
最后,我可能会被拒绝,因为我非常喜欢NetBSD,但您也应该尝试一下Glib的内置测试框架:
我对单元测试框架还是新手,但最近我尝试了,而且。这似乎与其他一些人的经历相反(参见前面的问题),但我发现cunit是最容易开始的。对我来说,这似乎也是一个不错的选择,因为cunit应该与其他xunit框架保持良好的一致性,我也经常切换语言。我很高兴上次需要单元测试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"));
}
我曾与一些和它非常容易设置。它被一些大型活动项目使用,如。下面是一个简单的失败if行示例:
fail_if (0 == get_element_position(spot), "Position should not be 0");
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;
}
还有,这是一个非常简单且易于使用的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
}
+1对bdhar的评论。如果您在搜索SO/Google时遇到问题,那么单元测试是您最不担心的:-)在相关链接中至少有两个副本…可能是