Software test coverage is very important in software development. The test ensures that the software runs as expected, and the test coverage is ensured that you check all the possibilities of the code. The two complement each other. Many modern languages have libraries or tools that generate coverage of coverage, but do you know that there is a "native" tool for C language?
Let us start with a simple code that returns ± 42 code:
<code class="language-c">#include <stdbool.h> int return_42(bool ret_neg) { if (ret_neg) return -42; return 42; }</code>
Now is a simple test file:
<code class="language-c">#include <assert.h> #include <stdbool.h> #include <stdio.h> int return_42(bool ret_neg); int main() { assert(return_42(false) == 42); assert(return_42(true) == -42); puts("Nice"); return 0; }</code>
Now, we can use the GCC/CC compiler and -fprofile-arcs
and -ftest-coverage
to compile code.
This will generate a file with executable files and extended name .gcno
.
After creating these files, we run the test and create a file with extension .gcda
after the operation.
With these newly created files, we can use GCOV tools to process them (please install GCOV first), then execute GCOV and pass in .gcda
and .gcno
file names (usually they have the same name as .c
files, the same name, the files in the same name, the same name, the files in the same name, the file, the same name, the file, the file in the same name, the file, the file, the file, the file, the file, the file, the file, the file, the file, the file, the file, the file, the file, the file, the file, the file, the file, the file, the file is the same name, the file, But it may be different). This will generate a file with an extension .gcov
. GCOV will provide a small summary about coverage, but for more than a simple test (for example, I have more than 120 tests in a library), we need to display the results more intuitive.
Next, we will use LCOV (please install LCOV first), and use -c
, -d .
and -o foobar.info
to run LCOV. It will generate a file called and print the coverage of the coverage. Unfortunately, this summary does not show which functions have been checked, but with the created foobar.info
file, we can use .info
to run LCOV, which will print a more readable form summary. However, we still only get one percentage, and we cannot see which lines have been checked, so let us enter the last step, making the results easier to visualize. -l foobar.info
and foobar.info
mark. Genhtml will generate a folder that contains more readable coverage (you can open this HTML file at will. My personal favorite way to open is to use -o bar_html
). python -m http.server -d bar_html
You can also use CLANG, but the mark will change. Since I do n’t like CLANG, and I feel that the final result is not beautiful, I did n’t study in -depth, but as far as I tried, all functions could run with LLVM. You only need to install two LLVM tools.
Finally, Genhtml has many customized marks. My commonly used markers are as follows:
Okay, here is here today, see you next time!
The above is the detailed content of C TEST COVERAGE IN C IS AND WILL TEACH YOU how to use. For more information, please follow other related articles on the PHP Chinese website!