使用unity进行测试驱动开发

前端之家收集整理的这篇文章主要介绍了使用unity进行测试驱动开发前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Unity介绍
Unity 是一个用纯C语言编写的测试工具. 它简洁实用,多应用于嵌入式系统.Unity工具可以裁剪用于各种规模的嵌入式项目,当然,只要是纯C语言的项目,Unity都可以使用.

主页:http://throwtheswitch.org/
源码下载:https://github.com/ThrowTheSwitch/Unity/downloads
github主页:https://github.com/ThrowTheSwitch/Unity

unity实践
1、准备待测源码、测试文件及测试目录(见第5步),下载源码到 /root/TDD 下
(懒孩子们请到这里下载

2、解压,并拷贝到test目录下


3、进入 /root/TDD/proj/test/ 使用自动生成脚本生成测试容器(Runner),现在多出来两个 *_Runner.c 文件

4、执行make进行测试

5、第一步中准备的src待测文件及测试文件如下
demo.h
#include <stdio.h>

extern int get_add_1(int);
extern int get_sub_1(int);

demo.c
#include "demo.h"

int get_add_1(int a)
{
   return (a+1);
}

int get_sub_1(int a)
{
   return (a-1);
}

demo_test_group1.c
#include "demo.h"
#include "unity.h"

void setUp(void)
{
   //printf("%s is running!\n",__FUNCTION__);
}

void tearDown(void)
{
   //printf("%s is running!\n",__FUNCTION__);
}

void testcase1(void)
{
   TEST_ASSERT_EQUAL(123,get_add_1(122));
}

void testcase2(void)
{
   TEST_ASSERT_EQUAL(123,get_add_1(122));
}

void test_case3(void)
{
   TEST_ASSERT_EQUAL(121,get_sub_1(122));
} 

demo_test_group2.c
#include "demo.h"
#include "unity.h"

void setUp(void)
{
   //printf("%s is running!\n",__FUNCTION__);
}

void test_case1(void)
{
   TEST_IGNORE_MESSAGE("test group2-case1 is ignored!");
   TEST_ASSERT_EQUAL(123,get_add_1(122));
}

void test_case2(void)
{
   TEST_IGNORE();
   TEST_ASSERT_EQUAL(123,get_add_1(122));
}

void test_case3(void)
{
   TEST_ASSERT_EQUAL(122,get_sub_1(122));
}

makefile
@H_502_52@# ========================================== # Unity Project - A Test Framework for C # Copyright (c) 2007 Mike Karlesky,Mark VanderVoord,Greg Williams # [Released under MIT License. Please refer to license.txt for details] # ========================================== C_COMPILER=gcc TARGET_BASE1=demo_test_group1 TARGET_BASE2=demo_test_group2 ifeq ($(OS),Windows_NT) TARGET_EXTENSION=.exe else TARGET_EXTENSION=.out endif TARGET1 = $(TARGET_BASE1)$(TARGET_EXTENSION) TARGET2 = $(TARGET_BASE2)$(TARGET_EXTENSION) SRC_FILES1=unity/src/unity.c ../src/demo.c ./demo_test_group1.c ./demo_test_group1_Runner.c SRC_FILES2=unity/src/unity.c ../src/demo.c ./demo_test_group2.c ./demo_test_group2_Runner.c INC_DIRS=-Isrc -Iunity/src -Iunity/extras/fixture/src/ -Iunity/extras/fixture/test/ -I../src/ SYMBOLS=-DTEST ifeq ($(OS),Windows_NT) CLEANUP = del /F /Q build\* && del /F /Q $(TARGET1) && del /F /Q $(TARGET2) else CLEANUP = rm -f build/*.o ; rm -f $(TARGET1) ; rm -f $(TARGET2) endif all: clean default default: # ruby auto/generate_test_runner.rb test/TestProductionCode.c test/no_ruby/TestProductionCode_Runner.c @$(C_COMPILER) $(INC_DIRS) $(SYMBOLS) $(SRC_FILES1) -o $(TARGET1) @$(C_COMPILER) $(INC_DIRS) $(SYMBOLS) $(SRC_FILES2) -o $(TARGET2) $(TARGET1) $(TARGET2) clean: @$(CLEANUP)

猜你在找的设计模式相关文章