How to unit test C (using code blocks)?

I have a unit test task for Project C for homework. It is written in blocks of code. Here is one example from the code:

void ServerUserWrite(int Command) //Command "1" prints an extra row into server. For example addinga new user. it expects that the extra row with the correct data is already existing in the structure. { FILE *UserDataBase; int i,j; UserDataBase=fopen(UserDatabasePath,"w"); if(Command==1) {ServerUserCount=ServerUserCount+1;} fprintf(UserDataBase,"%d\n",ServerUserCount); if(ServerUserCount>0) { for(i=0;i<ServerUserCount;i++) { fprintf(UserDataBase,"%d ",UserDB[i].UserID); fprintf(UserDataBase,"%s ",UserDB[i].User); fprintf(UserDataBase,"%d ",UserDB[i].UserPasswordLength); fprintf(UserDataBase,"%d ",UserDB[i].Encrypter); for (j=0;j<UserDB[i].UserPasswordLength;j++) {fprintf(UserDataBase,"%d ",UserDB[i].Pass[j]);} fprintf(UserDataBase,"%d ",UserDB[i].BackgroundColor); fprintf(UserDataBase,"%d ",UserDB[i].ForegroundColor); fprintf(UserDataBase,"%d ",UserDB[i].UnreadMessages); fprintf(UserDataBase,"%d\n",UserDB[i].UnreadTweets); } } fclose(UserDataBase); } 

Well, the question is: Is there any unit testing system for combining with blocks of code? And how to do it?

+4
source share
3 answers

I don't know about code blocks, but you can use check.h and follow this guide: http://check.sourceforge.net/doc/check_html/check_3.html . Writing test suites with it is pretty easy. I found out about this by looking at gstreamer-editing-services, you might want to look at their test suite: http://cgit.freedesktop.org/gstreamer/gst-editing-services/tree/tests/check/ges They redefined it but the way it works is basically the same.

0
source

Yes, we use Check to unit test our C project, there is also no need to integrate with the IDE, it is more friendly to show the test result as plain text.

But there is an environment for C ++ unit test, which can be combined with the IDE of the code block: Unit tests for the code block

+1
source

The grand dad of all unit tests in C questions is here:

C code testing module

Again, not specific to code blocks, but most strategies are standard.

0
source

Source: https://habr.com/ru/post/1384519/


All Articles