Unity C 语言单元测试的构建工具 Ceedling 使用详解

本文详细介绍了如何在Windows环境下安装Ceedling,包括解决Ruby Gem源被墙的问题,以及创建新项目、编写测试和运行测试的步骤。Ceedling是一个用于C语言的测试驱动开发(TDD)工具,通过它,开发者可以方便地创建和运行测试用例。文章还列举了常用Ceedling命令,帮助读者快速上手。
摘要由CSDN通过智能技术生成

1. 安装 Ceedling展开目录

安装 Ceedling 之前,首先需要下载安装 Ruby (版本号大于 1.8.6) 和 TDM-GCC ,安装完成后,即可安装 Ceedling ,打开系统命令行,输入

 
  • gem install ceedling

等待直至出现

 
  • Successfully installed ceedling-0.28.2

  • Parsing documentation for ceedling-0.28.2

  • Done installing documentation for ceedling after 1 seconds

  • 1 gem installed

即安装完成。

如果安装出错

 
  • ERROR: Could not find a valid gem 'ceedling' (>= 0), here is why:

  • Unable to download data from https://rubygems.org/ - SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed (https://api.rubygems.org/specs.4.8.gz)

说明国外的 gem 源被墙了,需要把 gem 源换成国内节点

删除原 gem 源

 
  • gem sources --remove https://rubygems.org/

添加国内 gem 源

 
  • gem sources -a http://gems.ruby-china.org/

试试打印是否替换成功

 
  • gem sources -l

如果成功则显示

 
  • http://gems.ruby-china.org/

然后重新安装 ceedling 即可。

2. 创建新项目展开目录

在系统命令行中 cd 到希望创建项目的目录,如

 
  • cd E:\Personal\Liuguo\TDD_test

然后再输入命令(如项目名为 MyNewProject )

 
  • ceedling new MyNewProject

等待创建完成,出现

 
  • Project 'MyNewProject' created!

  • - Tool documentation is located in vendor/ceedling/docs

  • - Execute 'ceedling help' to view available test & build tasks

即创建成功,此时的工程一级目录为

 
  • ├── build

  • ├── project.yml

  • ├── src

  • ├── test

  • └── vendor

各个目录的作用如下:

  • build – 构建目录
  • project.yml – Ceedling 配置文件
  • src – 源文件(.c 和 .h)
  • test – 测试文件
  • vendor – Ceedling 工具
注意:Ceedling 0.28.1 版本之后创建项目将不会出现 rakefile.rb 文件,相应的,原命令创建测试 rake module:create[point] 和 测试 rake test:all 也都被替换成 ceedling module:create[point] 和 ceedling test:all 。

3. 开始测试展开目录

先进入工程目录,即在系统命令行中输入命令

 
  • cd MyNewProject

再创建新测试(如新测试名叫 NewTest)

 
  • ceedling module:create[NewTest]

等待直到出现

 
  • File src/NewTest.c created

  • File src/NewTest.h created

  • File test/test_NewTest.c created

  • Generate Complete

即创建完成,然后键入

 
  • ceedling test:all

或直接输入

 
  • ceedling

来测试所有文件,当输出为

 
  • Test 'test_NewTest.c'

  • -------------------

  • Generating runner for test_NewTest.c...

  • Compiling test_NewTest_runner.c...

  • Compiling test_NewTest.c...

  • Compiling unity.c...

  • Compiling NewTest.c...

  • Compiling cmock.c...

  • Linking test_NewTest.out...

  • Running test_NewTest.out...

  •  
  • --------------------

  • IGNORED TEST SUMMARY

  • --------------------

  • [test_NewTest.c]

  • Test: test_NewTest_NeedToImplement

  • At line (14): "Need to Implement point"

  •  
  • --------------------

  • OVERALL TEST SUMMARY

  • --------------------

  • TESTED: 1

  • PASSED: 0

  • FAILED: 0

  • IGNORED: 1

时,说明新的测试已经配置完成。

接下来只要在 NewTest.c 、 NewTest.h 和 test_NewTest.c 中添加代码即可。

注:函数在不同的 module 中可以重名

4. 其他命令展开目录

在命令行中输入

 
  • ceedling help

即可查看 ceedling 的所有用法,

 
  • ceedling clean # Delete all build artifacts and temp...

  • ceedling clobber # Delete all generated files (and bui...

  • ceedling environment # List all configured environment var...

  • ceedling files:header # List all collected header files

  • ceedling files:source # List all collected source files

  • ceedling files:test # List all collected test files

  • ceedling logging # Enable logging

  • ceedling module:create[module_path] # Generate module (source, header and...

  • ceedling module:destroy[module_path] # Destroy module (source, header and ...

  • ceedling paths:source # List all collected source paths

  • ceedling paths:support # List all collected support paths

  • ceedling paths:test # List all collected test paths

  • ceedling summary # Execute plugin result summaries (no...

  • ceedling test:* # Run single test ([*] real test or s...

  • ceedling test:all # Run all unit tests (also just 'test...

  • ceedling test:delta # Run tests for changed files

  • ceedling test:path[dir] # Run tests whose test path contains ...

  • ceedling test:pattern[regex] # Run tests by matching regular expre...

  • ceedling verbosity[level] # Set verbose output (silent:[0] - ob...

  • ceedling version # Display build environment version info

转载出处: https://www.liuguogy.com/archives/Unity-C-build-system-Ceedling.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值