unity自动化测试框架

unity官网 http://www.throwtheswitch.org/unity
github https://github.com/ThrowTheSwitch/Unity

unity框架:
unity.c
unity.h
unity_internals.h

fixture夹具部分
unity_fixture.c
unity_fixture.h
unity_fixture_internals.h
unity_fixture_malloc_overrides.h

#include "unity_fixture.h"
TEST_GROUP(sprintf);

static char output[100];
static const char *expected;
/* 配置基本信息,可以避免在testcase中的重复 */
TEST_SETUP(sprintf)
{
memset(output, 0xaa, sizeof output);
expected = "";
}
/* 清理 */
TEST_TEAR_DOWN(sprintf)
{

}

static void expect(const char *s)
{
expected = s;
}

static void given(int charsWritten)
{
TEST_ASSERT_EQUAL(strlen(expected), charWritten);
TEST_ASSERT_EQUAL_STRING(expected, output);
TEST_ASSERT_BYTES_EQUAL(0xaa, output[strlen(expected) + 1]);
}

TEST(sprintf, NoFormatOperations)
{
expect("hey");
given(sprintf(output,"hey"));
}

TEST(sprintf, InsertString)
{
expect("Hello World\n");
given(sprintf(output,"Hello %s\n", "World"));
}

TEST_GROUP_RUNNER(sprintf)
{
RUN_TEST_CASE(sprintf, NoFormatOperations);
RUN_TEST_CASE(sprintf, InsertString);
}

TEST的入口函数是TEST_GROUP_RUNNER,这个函数的执行顺序如下:
TEST_SETUP(sprintf);
TEST(sprintf, NoFormatOperations);
TEST_TEAR_DOWN(sprintf);
TEST_SETUP(sprintf);
TEST(sprintf, InsertString);
TEST_TEAR_DOWN(sprintf);
TEST_GROUP_RUNNER入口为RunAllTests:
#include "unity_fixture.h"
static void RunAllTests(void)
{
RUN_TEST_GROUP(sprintf);
}

static   char   *argstring[] =
{
     "main" ,
     "-v" ,
};

int main(int argc, char *argv[])
{
return UnityMain(argc, argv, RunAllTests);
}
注释:-v开启冗余模式,运行测试用例时输出更多的信息

打桩与测试替身一样,在产品代码未完成时,或是与硬件相关时,可以模仿一些函数、数据、模块或库。

你可能感兴趣的:(test)