user1079520
user1079520

Reputation: 53

How to unit test C (with the help of code blocks)?

I have a task of unit testing C project for homework. It's written in Code Blocks. Here's 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 framework to combine with Code Blocks? And how to do it?

Upvotes: 5

Views: 4501

Answers (3)

Louis
Louis

Reputation: 427

Yes we use Check to unit test our C project too, there is no need to integrate to the IDE, it's more friendly to show the testing result as a plain text.

But there is a framework for C++ unit test which can combined with code block IDE: Unit testing for Code Block

Upvotes: 1

Barton Chittenden
Barton Chittenden

Reputation: 4416

The grand-daddy of all unit-testing in C questions is here:

Unit Testing C Code

Again, not specific to Code Blocks, but most of the strategies are standard C.

Upvotes: 0

Mathieu_Du
Mathieu_Du

Reputation: 827

Don't know about Code Blocks, but you might wanna use check.h and follow this tutorial : http://check.sourceforge.net/doc/check_html/check_3.html. Writing test suites with it is pretty easy. I learnt about it looking at gstreamer-editing-services, may'be will you wanna have a look at their test suite: http://cgit.freedesktop.org/gstreamer/gst-editing-services/tree/tests/check/ges They reimplemented it but the way it works is basically the same.

Upvotes: 0

Related Questions