You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This repo covers setting up a basic testing suite with github badges for a C/C++ library. Its not meant to be deep tutorial on testing but just cover some basics of setting up unit tests, coverage tests, and continuous integration (in this case using Travis-CI). The repo doesn't have a lot of code - there is a simple library which is tested for coverage and integration.
8
8
9
9
### Motivation
10
+
I just wanted to make a small standalone test project to see tools and workflow for C (or C++) language testing.
10
11
11
-
I just wanted to make a small standalone test project to see tools and workflow for C language testing.
The lib.h / lib.c files are broken out as examples of testing an embedded library. Most of the projects I work on are for embedded systems so I wanted a way to get a build badge for these embedded projects. Since many of those compilers and environments are not on Linux I wanted just a simple abstraction of how the Travis build project works without all the complexities of a "real" project.
@@ -46,7 +44,7 @@ Unit Testing is a practice of writting small tests to see that piece of code, ty
46
44
47
45
Note that its not the goal to create a test that passes every possible permutation of the input parameters - as this could be an impossibly large number or variations even for just a few parameters. This idea of testing all the possible paths of exeuction is called code coverage. Testing code coverage is done with tools which see if the test program has successfully "challenged" the target library code by examing whether each execution path (or line of code) has been run. For example if there is a function like this:
48
46
49
-
```
47
+
```C
50
48
intadd5ifGreaterThan2 (int a) {
51
49
int r;
52
50
@@ -60,15 +58,15 @@ int add5ifGreaterThan2 (int a) {
60
58
```
61
59
62
60
Our test program for add5ifGreaterThan2() needs to supply values of a that are both less and great than 2 so both paths of the if statement
63
-
```
61
+
```C
64
62
if (a<2)
65
63
```
66
64
67
65
are tested.
68
66
69
67
We do this with test code such as this:
70
68
71
-
```
69
+
```C
72
70
//code in test program ...
73
71
ASSERT (add5ifGreaterThan2(1) == 1) // supplies value of 'a' that tests the if (a<2) case
74
72
ASSERT (add5ifGreaterThan2(3) == 8) // supplies value of 'a' that tests the if (a>2) case
@@ -105,7 +103,7 @@ Here is the link to the project source
105
103
On Ubuntu Linux you can install gtest using this command. If you are developing on another sytem refer to the documentation link for install procedures. Other than installing, all of the commands and test procedures we'll be using later will be the same (whether Windows / MacOS / POSIX / Linux).
106
104
107
105
108
-
```
106
+
```bash
109
107
sudo apt-get install libgtest-dev
110
108
111
109
sudo apt-get install cmake # install cmake
@@ -149,7 +147,7 @@ Travis-CI then runs the example.out and looks for the exit code from the main()
149
147
## Code Coverage
150
148
Code coverage is achieved using gcov from the gcc test suite. The example.out test program is compiled with the flags -ftest-coverage -fprofile-arcs. To see the code coverage run gcov:
0 commit comments