CUnit Intro
CUnit Intro
Unit Testing
Informationsteknologi Code that isnt tested doesnt work Code that isnt regression tested suffers from code rot (breaks eventually) A unit testing framework enables efficient and effective unit & regression testing
Integration testing
Testing a module of code (e.g. a package)
Application testing
Testing the code as the user would see it (black box)
Conventionally
Informationsteknologi Ad hoc manner
Manual stimulation & observation E.g. adding a main method to a class, which runs tests on the class Uncomenting or deleting test code / drivers / printf /#ifdefs Assert and debug builds
Code that isnt tested doesnt work If code has no automated test case written for it to prove that it works, it must be assumed not to work.
Regression testing
Informationsteknologi New code and changes to old code can affect the rest of the code base
Affect sometimes means break
Regression = Relapsed to a less perfect or developed state. Regression testing: Test that code has not regressed Regression testing is required for a stable, maintainable code base
Refactoring
Informationsteknologi Refactoring is a behavior preserving transformation Refactoring is an excellent way to break code. Regression testing allows developers to refactor safely if the refactored code passes the test suite, it works
Axiom:
Code that isnt tested doesnt work If code has no automated test case written for it to prove that it works, it must be assumed not to work.
CUnit Testing
Informationsteknologi Each method is tested while developed
Create tests first Start with simplest that works Incrementally add code while testing
myUnit.c
Ccompiler
myUnitTests.exe
myUnitTests.c Test-report.xml
Creating a Test
Informationsteknologi Implement test functions Run the test using a TestRunner Group multiple TestCases using TestSuite
What is xUnit?
Informationsteknologi A set of Frameworks for programming and automated execution of test-cases X stands for programming language
Most Famous is J-UNIT for Java But exists for almost all programming languages C-unit, Cutest, Cpp-Unit, JUnit N-unit,
xUNIT principles
Informationsteknologi
Write test suite for each unit in the program. All test can be executed (automatically) at any time. For each program modification all tests must be passed before the modification is regarded as complete - regression testing Test First implement later! Originally based on eXtreme Programming principles: TDD (Test Driven Development) cycle
1. 2. 3.
Write test case, and check it fails Write the new code Check that the test passes (and maybe refactor, re-test)
Core parts
TestRunner
Informationsteknologi
runs
TestResult
Collects results
MyTest
TestFixture
uses
Test Suite
Test Case
Concepts
Assertions Informationsteknologi
Boolean expression that compares expected and actual results The basic and smallest building-block
Test Case
A composition of concrete test procedures May contain several assertions and test for several test objectives E.g all test of a particular function
Test Suite
Collection of related test cases Can be executed automatically in a single command
// Registers the fixture into the 'registry' CU_pSuite getTriangleSuite(){ CU_pSuite pSuite = NULL; if ((NULL == CU_add_test(pSuite, "Tests classification of valid triangles", validClassification)) || (NULL == CU_add_test(pSuite, "Tests classification of invalid triangles", invalidClassification)) || (NULL == CU_add_test(pSuite, "Tests for string conversion", invalidClassification)) || (NULL == CU_add_test(pSuite, "Tests triangle main driver", testCheckTriangle)) ){ . . .}
Assertion Examples
Informationsteknologi
CU_ASSERT_EQUAL(rectangularTriangle, classifyTriangle(13,12,5) ); int actual_val; CU_ASSERT(stringToInt("+0",&actual_val)); CPPUNIT_ASSERT_EQUAL(0, actual_val ); char* argv4[4]= {programName,"1","1","2"}; CU_ASSERT_EQUAL(string( "Isosceles Triangle"), string(checkTriangle(4,argv4)));
Driver File
Informationsteknologi
int RunAllTests(void) { CU_pSuite pSuite = NULL; pSuite=getTriangleSuite(); CU_set_output_filename("TriangleTest"); CU_list_tests_to_file(); CU_automated_run_tests(); } int main(int argc, char* argv[]) { return RunAllTests(); }
Test suite
Informationsteknologi
Collection of test cases (or other test suites) in a logical unit Test Suites can be executed automatically
Test Reports
Informationsteknologi
C:\NovoUnitTest\TriangleDemo\cppunitDemo>Debug\cppunitDemo.exe .F... c:\novounittest\triangledemo\testtriangle\testtriangle.cpp(30):Assertion Test name: TriangleTests::validClassification equality assertion failed - Expected: 1 - Actual : 4 Failures !!! Run: 4 Failure total: 1
Failures: 1
Errors: 0
Informationsteknologi
Advice: Application
Informationsteknologi Design and program for testability Directly applicable to
Pure function libraries API
(With some footwork also user interfaces, network-, web-, and database applications)
Conclusions
Code that isnt tested doesnt work Code that isnt regression tested suffers from code rot (breaks eventually) A unit testing framework enables efficient and effective unit & regression testing Use xUNIT to store and maintain all the small tests that you write anyway Write tests instead of playing with debugger and printf tests can be automatically repeated Informationsteknologi