Unity单元测试

Unity单元测试是一个专门用于嵌入式单元测试的库, 现在简单讲下移植以及代码结构.

源码地址: GitHub - ThrowTheSwitch/Unity: Simple Unit Testing for C

1.我们只需要移植三个文件即可: unity.c, unity.h, unity_internals.h

2.然后添加需要测试的函数.

3.在main.c中添加两个空函数以及一个宏

cpp 复制代码
/* 这两个空函数需要定义, 否则编译不过 */
void setUp()
{
}
void tearDown()
{
}

/* 运行测试, 源码已经定义好了 */
#define RUN_TEST(TestFunc, TestLineNum) \
{ \
  Unity.CurrentTestName = #TestFunc; \
  Unity.CurrentTestLineNumber = TestLineNum; \
  Unity.NumberOfTests++; \
  if (TEST_PROTECT()) \
  { \
      setUp(); \
      TestFunc(); \
  } \
  if (TEST_PROTECT()) \
  { \
    tearDown(); \
  } \
  UnityConcludeTest(); \
}

废话就不多说了, 我直接把我的工程放上来:

https://download.csdn.net/download/qq_38591801/88856781

我这个demo是用mingw在window环境下编译的, 大家也可以使用visual studio移植, 这样就不用管makefile文件了, 但是使用visual studio真心的太笨重了, 运行也慢, 强烈建议大家使用vscode+gcc编译(在windows环境下用vscode配置gcc编译代码_windows vscode gcc-CSDN博客)

测试过程中, 如果一个函数测试未通过, 那么这一批里面的函数后面的函数是不会运行的

cpp 复制代码
#include "unity.h"
#include "test.h"

int demo()
{
    printf("this is you need to test function\n");
    return 1;
}
int demo1()
{
    printf("this is you need to test function\n");
    return 0;
}
void test_func(void)
{
    TEST_ASSERT_EQUAL(0, demo());//运行到错误后, 后面的函数就不会运行了
    TEST_ASSERT_EQUAL(0, demo1());
}
void test_func1(void)
{
    TEST_ASSERT_EQUAL(0, demo1());
}

更多详细的测试可以参考Unity的例程.

相关推荐
Sator12 小时前
Unity AStarPath的踩坑点
unity
榮華5 小时前
DOTA全图透视辅助下载DOTA全图科技辅助下载DOTA外挂下载魔兽争霸WAR3全图下载
数据库·科技·游戏·游戏引擎·游戏程序·ai编程·腾讯云ai代码助手
还是大剑师兰特8 小时前
Vitest单元测试教程
单元测试
RPGMZ9 小时前
RPGMakerMZ 游戏引擎 野外采集点制作
javascript·游戏·游戏引擎·rpgmz·野外采集点
星河耀银海9 小时前
Unity基础:摄像机Camera的参数设置与视角控制
unity·游戏引擎·lucene
星河耀银海9 小时前
Unity基础:Transform组件的位移、旋转与缩放详解
unity·游戏引擎·lucene
weixin_4093831219 小时前
godot 击败敌人后增加经验的脚本
游戏引擎·godot
海清河晏1111 天前
数据结构 | 单链表
数据结构·unity·dreamweaver
mxwin1 天前
Unity URP 下 MatCap 技术详解 无视光照环境的卡通与质感渲染方案
unity·游戏引擎
weixin_409383121 天前
godot 获取敌人位置自动发射子弹 旋转枪口
游戏引擎·godot