加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 程序设计 > 正文

使用unity进行测试驱动开发

发布时间:2020-05-27 20:33:47 所属栏目:程序设计 来源:互联网
导读:Unity介绍 Unity 是一个用纯C语言编写的测试工具. 它简洁实用,多应用于嵌入式系统.Unity工具可以裁剪用于各种规模的嵌入式项目,当然,只要是纯C语言的项目,Unity都可以使用. 主页:http://throwtheswitch.org/ 源码下载:https://github.com/ThrowTheSwitch/Un

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
# ==========================================
#   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)

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读