没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
Welcome Maybe you've heard of Test-Driven Development (TDD), and maybe you've even thought it seemed like a reasonable idea. If you haven't tried TDD yet though, you really should. This guide contains step-by-step examples to get you started test driving in C, especially for embedded software applications. We'll look at how to use the unit test framework called Ceedling to help us do this. In the first example, we'll see how to create tests and write the code to make them pass. In the second example we look at mocking, and learn how to use it simulate our hardware. All the tests in these examples compile and run on your host PC (with GCC), with no target hardware needed.
资源推荐
资源详情
资源评论
Contents
Welcome.......................................................................................................................................................................... 3
What is Test-Driven Development (TDD)?..........................................................................................................4
The Tools.......................................................................................................................................................................... 5
Installing Ceedling.......................................................................................................................................................6
Creating a New Ceedling Project...........................................................................................................................7
Example #1: Starting TDD with Ceedling............................................................................................................9
Create a Module...................................................................................................................................................... 9
Implement a Feature...........................................................................................................................................11
Repeat....................................................................................................................................................................... 13
Refactoring.............................................................................................................................................................. 15
Mocking Hardware Interfaces...............................................................................................................................17
Example #2: Mocking a Hardware Interface with CMock..........................................................................19
Create the Temperature Sensor Module.....................................................................................................19
Write Our First Test..............................................................................................................................................19
Create the Function Under Test......................................................................................................................21
Mock the I2C Interface.......................................................................................................................................21
Implement the Function Under Test.............................................................................................................22
Adding Another Test...........................................................................................................................................22
Example #3: Add Unit Tests to Your Current Project with Ceedling......................................................24
Start with an Existing Project...........................................................................................................................25
Install and Configure Ceedling.......................................................................................................................26
Create a New Test File........................................................................................................................................29
Getting it to Build.................................................................................................................................................29
Adding More Source Folders...........................................................................................................................30
Mocking Hardware Drivers from the Header Files..................................................................................31
CMock Won't Do Paths to Header Files......................................................................................................32
Including Other Header Files in Our Mocks..............................................................................................33
Enabling Mocks for Extern-ed Function Prototypes...............................................................................34
Add an Actual Unit Test.....................................................................................................................................36
The Next Steps......................................................................................................................................................37
References.................................................................................................................................................................... 39
Source Code...........................................................................................................................................................39
Documentation..................................................................................................................................................... 39
Ceedling Quick Reference......................................................................................................................................40
Test Assertions.......................................................................................................................................................40
Mock Function Formats.....................................................................................................................................41
How to Use Ceedling for Embedded Test-Driven Development 2
Welcome
Maybe you've heard of Test-Driven Development (TDD), and maybe you've even thought it
seemed like a reasonable idea. If you haven't tried TDD yet though, you really should.
This guide contains step-by-step examples to get you started test driving in C, especially for
embedded software applications.
We'll look at how to use the unit test framework called Ceedling to help us do this. In the first
example, we'll see how to create tests and write the code to make them pass. In the second
example we look at mocking, and learn how to use it simulate our hardware.
All the tests in these examples compile and run on your host PC (with GCC), with no target
hardware needed.
How to Use Ceedling for Embedded Test-Driven Development 3
What is Test-Driven Development (TDD)?
The premise of TDD is that we use the creation of unit tests to incrementally drive the
development of the software. The steps look like:
1. Write a test, and watch it fail.
2. Implement just enough code to make the test pass.
3. Refactor.
4. Repeat.
This allows us to be very clear about what the code is to do, because we've defined every
behavior in a test.
With each iteration you add a bit more functionality to your software, and the tests give you
confidence that you're doing it correctly.
One of the difficulties of unit testing is that it takes some degree of experience to write
testable code. If you write the tests first though, you'll figure out how to make your code
testable while you write it. This means you won't waste your time with this problem at all!
For me, the greatest aspect of TDD is that it takes a big problem to solve (how to implement a
software application) and reduces it to a simple problem (what is the next little thing I need
this software to do). Then I just write a test for that feature, implement it and repeat.
You simply stop when you don't need any more features. And when you do stop, you're
confident that what you have is working exactly how you want it to. It's unlikely that you'll be
spending a lot of time chasing down bugs.
How to Use Ceedling for Embedded Test-Driven Development 4
The Tools
The testing tools used in this example are Ceedling, Unity and CMock. These are the best C
unit test tools available, from the people over at http://www.throwtheswitch.org/.
Ceedling is an automated testing framework for C applications.
In order to do TDD, you need to be able to create and run tests easily since you'll be doing it
all the time. Ceedling provides automatic test discovery, mock generation and test execution,
which makes it the best option for unit testing in C. It also builds and runs tests on the host
PC, so when working on an embedded project we don't have to waste time downloading to
the target.
Unity is the unit test framework provided with Ceedling. It gives us all of our test assertions
constructing our tests.
CMock is the mocking framework used with Ceedling. The mocking framework is what lets us
simulate interactions with other software modules, so that we can test our software units in
isolation.
How to Use Ceedling for Embedded Test-Driven Development 5
剩余40页未读,继续阅读
资源评论
MichaelNZ
- 粉丝: 6
- 资源: 43
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 毕设和企业适用springboot企业数据管理平台类及跨平台数据交换系统源码+论文+视频.zip
- 毕设和企业适用springboot企业数据管理平台类及企业协作平台源码+论文+视频.zip
- 毕设和企业适用springboot企业数据管理平台类及企业管理智能化平台源码+论文+视频.zip
- 毕设和企业适用springboot企业数据管理平台类及情感分析平台源码+论文+视频.zip
- 毕设和企业适用springboot企业数据管理平台类及市场调查平台源码+论文+视频.zip
- 毕设和企业适用springboot企业数据管理平台类及社交媒体平台源码+论文+视频.zip
- 毕设和企业适用springboot企业数据管理平台类及视频内容分发平台源码+论文+视频.zip
- 毕设和企业适用springboot企业数据管理平台类及视频编辑平台源码+论文+视频.zip
- 毕设和企业适用springboot企业数据管理平台类及视频内容管理平台源码+论文+视频.zip
- 毕设和企业适用springboot企业数据管理平台类及视频直播系统源码+论文+视频.zip
- 毕设和企业适用springboot企业数据管理平台类及行业资讯平台源码+论文+视频.zip
- 毕设和企业适用springboot企业数据管理平台类及消费品管理平台源码+论文+视频.zip
- 毕设和企业适用springboot企业数据管理平台类及员工管理平台源码+论文+视频.zip
- 毕设和企业适用springboot企业数据管理平台类及虚拟银行平台源码+论文+视频.zip
- 毕设和企业适用springboot企业数据管理平台类及在线学习平台源码+论文+视频.zip
- 毕设和企业适用springboot企业数据管理平台类及直播流媒体平台源码+论文+视频.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功