当前位置:网站首页>The significance of code coverage testing to programming white and its application
The significance of code coverage testing to programming white and its application
2022-06-22 07:46:00 【Leon_ George】
Active address : Graduation season · The technique of attack er
ask : What open source projects have you used that you can't help sharing with friends ?
answer : Code coverage testing . You are new to the workplace , perky , imbued with a spirit that can conquer mountains and rivers ! after 4 Years and even 7 Years of painstaking efforts , Study hard , You've already got “ Call beast ” Abbot's biography . Little “ Jingwu Hall ” I can't hold your ordinary heart which is ready to move , So you sneak down the mountain , The abbot will say “ The tiger ”! finally , You understand a truth ——“ The tiger at the foot of the mountain can't eat people at all , But she will still kill you !”, The only way to procrastinate is to feed the tiger a food called Money Things that are . But this Money It is not easy to find , The person who owns it needs you to keep moving bricks for him . So you volunteered to join a mysterious organization , And get “ Code the agriculture ” Title . Since then, I have embarked on a road of no return . The so-called newborn calves are not afraid of tigers , With the martial arts learned from Abbot beast calling , You think it's easy to move this little brick . but , Jianghu is always more dangerous than you think . One is too voluminous ! You move at one time 10 block , Nash moved at one time 15 block , What is more hateful is the old Wang next door , In order to win the hearts of other people's Tigers , I moved in the daytime , Move at night , It also has a foreign digital name 996,007. I don't know , all 007 了 , There is no time to play with tigers ??? The second is too realistic , People will not take more because of you Money Go back to the tiger , The tiger is going to eat you , To teach you what sword manual to use will move a lot of bricks faster . But there are good people , Universal Linux Bodhisattva set up a guild , It's called the open source community . Where many kind-hearted people will share their unique sword manual with everyone , Anyone can use . But there is a condition —— If you practice this skill, you must “ autocastration ”................... If not “ autocastration ”, You can also practice this skill !!! I don't know , Almost ruined my first intention to go down the mountain . As a prostitute of the guild , I brought you a sword manual from someone else's family today —— Code coverage testing . This tool is so old , He can help you count how many bricks you move in a day , Those bricks were not counted by the supervisor , With it , Mother no longer has to worry about my study !——“ ha-ha , Can you understand what I said ? Understand nature and understand ! I don't understand , I'm getting it !”
- Don't fart much , Don't talk much ! Take off your pants , Go straight to the topic !
Code coverage testing
Code coverage is the ratio and degree of the code that has been executed in the test process to the total code prepared for the test , It focuses on executing use cases , What code has been executed to , What code has not been executed to . The main meaning is that you can analyze the uncovered part of the code , It is necessary to test whether the design is sufficient in the early stage ? Whether the code not covered is the blind spot of test design ? It's demand / The design is not clear enough ? Or is the test design misunderstood ?
Code coverage testing helps to find code that cannot be covered by multiple test cases , You can reverse the confusion in code design , Reminder design / Developers sort out the code logic , Improve code quality , At the same time, it also provides a basis for obsolete code .
Types of code coverage tests
- Function coverage : A function that describes how much proportion the program executes when it runs .
- Statement coverage : A statement that describes how much of a program is executed when it runs .
- Branch coverage : Describes how many branch statements are executed when the program runs (if-else、case、while-do etc. ).
- Conditional coverage : Condition judgment that describes how much proportion is executed when the program is running (if、while etc. ).
At present , stay GNU In the environment , Generally speaking, there are three common tools :gcov、lcov、gcovr
- gcov: yes gcc Part of , No separate installation is required , But only plain text code coverage files can be generated ;
- lcov: Independent of gcc Of , Separate installation required , Cross platform performance is not good , But it can generate HTML Format code coverage file ;
- gcovr: Independent of gcc Of , Separate installation required , But cross platform is good , The instructions are simple , It can also generate HTML Format code coverage file , Highly recommended ;
gcov
Introduce :gcov By gcc Code coverage generation tool provided by tool chain , It can be very convenient and GCC The compiler is used in conjunction with , Usually , Direct installation gcc Tool chain , It also includes gcov Command line tools .
For the work done by the code coverage tool , It can be simply understood as : Mark a run in progress , What code has been executed , Which are not implemented . therefore , Even without testing code , You can also get the code coverage by directly running the compiled products . It's just , Usually, the coverage is low .
gcov And what you get is Text form Of , And different source files need to be executed one by one gcov command , It is inconvenient for large projects , We hope to get more beautiful and easy to browse results .
Instructions for use :
- adopt gcc Compiler options for (
-fprofile-arcs -ftest-coverage) Get coverage information file . there “ Generate coverage information ” The steps of are exactly the same for any coverage analysis tool . - use gcov collect 、 Analyze coverage information files and generate code coverage reports in plain text format .
- adopt gcc Compiler options for (
Use examples :
Code under test :
/* file_name: profiling.c */ #include <stdio.h> #define MAX 50 float sum = 0.1234; float A[MAX] = { 1, 2, 3, 4, 5, 6, 7}; // Array float f(int x) { int i, sum; sum = 1; for (i = 0; i < 100*x; i++) sum = sum + i; return(sum); } float g(float x) { int i, sum; sum = 0; for (i = 0; i < 10*x; i++) sum = sum + f(i); return(sum); } int main(int argc, char *argv[]) { int i, j; for (i = 0; i < MAX; i = i + 1) { j = i; A[i] = 1/g(i+1); } sum = 0.0; for (i = 0; i < MAX; i = i + 1) { sum = sum + A[i]; } printf("sum = %f\n",sum); }compile : To generate a coverage information file , You must add
-fprofile-arcs -ftest-coverageCompilation options :gcc -fprofile-arcs -ftest-coverage profiling.c- After the compilation , We will get a **“ reform ” An executable program
a.out, When the program is compiled , Some records are inserted in its assembly file The number of executions per line Instructions , And one. The suffix is .gcno** The file ofprofiling.gcno, It is be gcov One of the key data files referenced . -ftest-coverage: Add... To the source code Record the number of single line code executions Instructions ;-fprofile-arc: Add instructions to the source code to record the execution times of each branch statement .
- After the compilation , We will get a **“ reform ” An executable program
Run the program :
- Run the executable generated in the previous step on the command line :
./a.out - After the program runs , Get one
profiling.gcdaThe file of , It andprofiling.gcnoThe same is about to be gcov Referenced data files ( Code coverage information file ).
- Run the executable generated in the previous step on the command line :
Generate code coverage reports
Run the coverage generation command on the command line :
$ gcov profiling.c File 'profiling.c' Lines executed:100.00% of 18 Creating 'profiling.c.gcov'After execution ,gcov The previous two data files will be referenced (
profiling.gcnoandprofiling.gcda) Give a total coverage value , And generate a code coverage reportprofiling.c.gcov:In the file
profiling.c.gcovView source code lines in 、 Functions 、 Execution times of each branch, etc :-: 0:Source:profiling.c -: 0:Graph:profiling.gcno -: 0:Data:profiling.gcda -: 0:Runs:1 -: 1:#include <stdio.h> -: 2: -: 3:#define MAX 50 -: 4: -: 5:float sum = 0.1234; -: 6:float A[MAX] = { 1, 2, 3, 4, 5, 6, 7}; // Array -: 7: -: 8: 12750: 9:float f(int x) -: 10:{ -: 11: int i, sum; -: 12: 12750: 13: sum = 1; 214000250: 14: for (i = 0; i < 100*x; i++) 213987500: 15: sum = sum + i; 12750: 16: return(sum); -: 17:} -: 18: 50: 19:float g(float x) -: 20:{ -: 21: int i, sum; -: 22: 50: 23: sum = 0; 12800: 24: for (i = 0; i < 10*x; i++) 12750: 25: sum = sum + f(i); 50: 26: return(sum); -: 27:} -: 28: -: 29: 1: 30:int main(int argc, char *argv[]) -: 31:{ -: 32: -: 33: int i, j; -: 34: 51: 35: for (i = 0; i < MAX; i = i + 1) -: 36: { 50: 37: j = i; 50: 38: A[i] = 1/g(i+1); -: 39: } -: 40: 1: 41: sum = 0.0; 51: 42: for (i = 0; i < MAX; i = i + 1) -: 43: { 50: 44: sum = sum + A[i]; -: 45: } -: 46: 1: 47: printf("sum = %f\n",sum); -: 48:}- If a statement is not overwritten ( perform ) To , Will adopt “#####” Symbol to identify .
- It can be seen that , Direct use gcov The generated code coverage report is not very intuitive .
lcov
Introduce :lcov yes gcov The graphical front end of the tool , Collection of multiple source files gcov data , Generate... That describes coverage HTML page . The generated results will include an overview page , Aspect browsing .
install :Ubuntu The installation command in the environment is :
sudo apt-get install lcovUsage method :
In the same way , Generate **.gcno and .gcda** Data files :
profiling.gcnoandprofiling.gcdaUse lcov The command calls the two data files mentioned above , obtain **.info** file :
$ lcov --capture --directory . --output-file profiling.info Capturing coverage data from . Found gcov version: 10.3.0 Using intermediate gcov format Scanning . for .gcda files ... Found 1 data files in . Processing profiling.gcda Finished .info-file creation $ ls a.out profiling.c profiling.c.gcov profiling.gcda profiling.gcno profiling.info- Or in short :
lcov -c -d . -o profiling.info
- Or in short :
Use genhtml take coverage.info Turn into HTML file , Coexist in the specified directory :
$ genhtml profiling.info -o html Reading data file ./profiling.info Found 1 entries. Found common filename prefix "/home/leon1124" Writing .css and .png files. Generating output. Processing file gcov/profiling.c Writing directory view page. Overall coverage rate: lines......: 100.0% (18 of 18 lines) functions..: 100.0% (3 of 3 functions) $ ls a.out html profiling.c profiling.c.gcov profiling.gcda profiling.gcno profiling.info $ cd html;ls amber.png gcov glass.png index-sort-l.html ruby.png updown.png emerald.png gcov.css index-sort-f.html index.html snow.png
lcov Introduction to common command options ( Can also pass –help Check it out. )
-c perhaps --capture : Specifies that coverage information is collected from the compiled product .
-d DIR perhaps --directory DIR : Specify the path to the compiled product .
-e FILE PATTERN perhaps --extract FILE PATTERN : From the specified file according to PATTERN Filter results .
-o FILENAME perhaps --output-file FILENAME : Specify the file name of the coverage output .
–rc lcov_branch_coverage=1 : Turn on branch coverage calculation (lcov Branch coverage is not turned on by default )
Be careful : In order to make html The report format also reflects the branch coverage results , In execution
genhtmlOn command , You also need to add this option :$ lcov -c -d . --rc lcov_branch_coverage=1 -o profiling.info $ genhtml --rc lcov_branch_coverage=1 -o html ./profiling.info Reading data file ./profiling.info Found 1 entries. Found common filename prefix "/home/leon1124" Writing .css and .png files. Generating output. Processing file gcov/profiling.c Writing directory view page. Overall coverage rate: lines......: 100.0% (18 of 18 lines) functions..: 100.0% (3 of 3 functions) branches...: 100.0% (8 of 8 branches)
gcovr
gcovr It's about C/C++ Code coverage and support in multiple ways ( Including list mode 、XML File mode 、HTML Web page mode, etc ) The tools shown , It can be used by some integration tools to parse .
gcovr Yes, it is Python Written , This means that as long as there is Python The environment can be used gcovr, Whether it's WINDOWS still LINUX.
install :
- Python Environmental use :
pip install gcovr - Linux Environmental use :
sudo apt install gcovr
- Python Environmental use :
Usage method :
Compile and generate **.gcda** Data files :
$ gcc --coverage -g -O0 profiling.c $ ls a.out profiling.c profiling.gcnoRun the generated executable , obtain **.gcda** file :
$ ./a.out sum = 0.000001 $ ls a.out profiling.c profiling.gcda profiling.gcnoRun directly under the current directory gcovr command , Get coverage report results in text form :
$ gcovr ------------------------------------------------------------------------------ GCC Code Coverage Report Directory: . ------------------------------------------------------------------------------ File Lines Exec Cover Missing ------------------------------------------------------------------------------ profiling.c 18 18 100% ------------------------------------------------------------------------------ TOTAL 18 18 100% ------------------------------------------------------------------------------By adding options , send gcovr The command to get html Coverage report results in the form of (
coverage.html coverage.profiling.c.html):$ gcovr --html-details -o coverage.html a.out coverage.html coverage.profiling.c.html profiling.c profiling.gcda profiling.gcno

Common compilation options :
-r RootPath perhaps --root RootPath, namely
rFollowed by the root directory of your project , The default is ’.', The current directory .-b perhaps --branches Report as branch coverage .
$ gcovr -b ------------------------------------------------------------------------------ GCC Code Coverage Report Directory: . ------------------------------------------------------------------------------ File Branches Taken Cover Missing ------------------------------------------------------------------------------ profiling.c 8 8 100% ------------------------------------------------------------------------------ TOTAL 8 8 100% -------------------------------------------------------------------------------x perhaps --xml The format of the specified report is XML.
$ gcovr --xml -o coverage.xml-o OUTPUT perhaps --output OUTPUT Specify the file name of the coverage output .
–html The format of the specified report is HTML.
Active address : Graduation season · The technique of attack er
边栏推荐
- Idea cannot connect to sqlsms
- Crmeb open version 4.3
- Vue page caching problem solving (keep alive + page level routing guard + lifecycle activated)
- Baidu Post Bar crawler crawls to the middle of the building
- 丰田bZ4X取消上市发布会,就算低温充电问题不存在,产品力如何?
- Cocoapods creates private libraries and publishes them
- Usage and understanding of async/await in JS
- Remote Desktop Manager
- Major enterprises are losing money one after another. Where will the environmental protection industry go?
- What are the ways for Taobao merchants to put their babies on the shelves in batches
猜你喜欢

Vue page caching problem solving (keep alive + page level routing guard + lifecycle activated)

How to upload Taobao tmall products with one click

Qualcomm platform msm8953 display subsystem learning

Reasons and solutions for Taobao's baby's prompt "sales attribute is required and parameter format is wrong"

Backup the method of uploading babies in Taobao stores to multiple stores

微信小游戏(四)

Get through version 4.3 mind map

Flutter gets the context, and flutter gets the DOM element location

Solution to the problem of "brand abuse such as brand inconsistency and stacking in the published product information" prompted by copying and uploading

记一次内网渗透测试实训总结
随机推荐
Wx applet vant UI call interface to realize two-level cascade
Invalid applet margin right, text overflow?
【宋红康 MySQL数据库 】【高级篇】【07】MySQL的存储引擎
Use multithreading to speed up your crawler
Itemtools permutation
navicat如何查询已连接的数据库密码信息
微信小游戏(三)
JS 数组扁平化 (递归写法)
JS gets all child nodes under a div natively
lr 2022超详细安装教程「最新」
White paper on Web page quality of Baidu search engine guides website construction and optimization
Impact of dead chain on websites
ES6 set data type de duplication of array, intersection, union and difference
Charles uses
The ranking of websites is very important for websites
[multi thread programming] thread scheduling strategy and priority
How to improve Baidu's weight
User defined pop-up use
Applet /vant UI to upload files
Scrollrect for tableview