Article 133 - Programming - Unit Test vs Functional Test vs Integration Test

Writting correct programs is a difficult challenge. Making use of different types of testing stratgies is useful to confirm one's program. This article discusses bthree types of test strategies: unit tests, functional tests, and integration tests.

Example Functions

The following is a fragment of code that will be used as part of this article's discussion.

Code that has not been tested.

int unTestedFunction( int * parameter ){
    (*parameter)++;
}

int anotherFunction( int * parameter ){
    int i = 5;
    for( ; i > 0; i-- ){
        unTestedFunction( parameter );
    }
}

Unit Test

Unit Test is the most basic form of the test strategies. These tests validate the written code for sanity correctness.

When a piece of code is written, each line of code should be validated for correctness. In modern programming languages, functions are written and then tested. For example, validating the input parameters for null pointers. Validating output is also ideal, especially for complex return values.

An example of Unit testing the untested code.

int unitTest(){
    //null pointer check
    unTestedFunction( 0 );
}

Often, Unit tests can overlap the role of Functional Test. Ultimately, the programmer should evaluate which group the test should be part of. A good strategy is that Unit Test should be fast and quick; if a test takes a long time, it should be considered a Functional Test.

Functional Test

Functional tests are an extension to Unit tests. These tests, however, are more involving and detailed. Instead of simple sanity validations like in Unit Test, functional correctness is verified.

The test should validate the algorithm of a particular section of code or function. For example, a Fibonacci function should be tested for logical cases, as well as extreme cases.

An example of Functional testing the untested code.

int functionalTest(){
    //verify output is input (parameter) plus 1
    int parameter = 5;
    unTestedFunction( &parameter );

    if( parameter != 6 ){
        //test failed!
    }
}

Sometimes, there can be a lot of cases to handle. It is the responsibility of the programmer to ensure that all cases are covered in these tests.

Integration Test

Integration tests are important; afterall, programs are often written by multiple engineers. Even if a single engineer is the sole owner of a program, fitting together different pieces of code requires delicate attention.

Care needs to be taken to ensure that segments of code (functions) play nicely. Even if all Unit tests and Functional tests have passed, it is by no means a representation that the program in its entirely can function as intended.

An example of Integration testing the untested code.

int integrationTest(){
    //verify another client (function) works properly
    int parameter = 5;
    anotherFunction( &parameter );

    if( parameter != 10 ){
        //test failed!
    }
}

Understanding the types of tests is critical and applying them will lead you to stronger coding habits.

Comments (0)

Post a comment

  • Name:
  • Post:
  • Challenge:

Register or login to post comments easier.


Vantasy World Copyright 2011 - 2023. Vantasy World is a project developed by Vantasy Online. Privacy Policy.