MENU

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

January 8, 2018 • Read: 1097 • 技术

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.cNewTest.htest_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
Leave a Comment

已有 1 条评论
  1. mmmmmmm mmmmmmm

    太牛逼了。