Introduction to C Unit Testing (CUnit) Brian Nielsen Arne Skou



Similar documents
Fail early, fail often, succeed sooner!

Approach of Unit testing with the help of JUnit

Agile.NET Development Test-driven Development using NUnit

JUnit. Introduction to Unit Testing in Java

Unit Testing. and. JUnit

Table of Contents. LESSON: The JUnit Test Tool...1. Subjects...2. Testing What JUnit Provides...4. JUnit Concepts...5

Unit Testing with zunit

Effective unit testing with JUnit

Unit Testing and JUnit

Unit Testing with JUnit and CppUnit

Test-Driven Development

Introduction to Automated Testing

Unit Testing webmethods Integrations using JUnit Practicing TDD for EAI projects

Test Driven Development

Java course - IAG0040. Unit testing & Agile Software Development

Test-Driven Development and Unit Testing with Parasoft Concerto

the first thing that comes to mind when you think about unit testing? If you re a Java developer, it s probably JUnit, since the

Unit Testing C++ Code CppUnit by Example Venkat Subramaniam

Objectives. Chapter 2: Operating-System Structures. Operating System Services (Cont.) Operating System Services. Operating System Services (Cont.

Test Automation Integration with Test Management QAComplete

Continuous Integration

Unit testing with JUnit and CPPUnit. Krzysztof Pietroszek

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Unit-testing with JML

Module 4: File Reading. Module 5: Database connection

Project 2: Bejeweled

Unit Testing JUnit and Clover

Logistics. Software Testing. Logistics. Logistics. Plan for this week. Before we begin. Project. Final exam. Questions?

Using JUnit in SAP NetWeaver Developer Studio

Licensed for viewing only. Printing is prohibited. For hard copies, please purchase from

Announcement. SOFT1902 Software Development Tools. Today s Lecture. Version Control. Multiple iterations. What is Version Control

Capabilities of a Java Test Execution Framework by Erick Griffin

Testing. Chapter. A Fresh Graduate s Guide to Software Development Tools and Technologies. CHAPTER AUTHORS Michael Atmadja Zhang Shuai Richard

Continuous integration for databases using Red Gate tools

Test Case Design Techniques

Testing, Debugging, and Verification

Software Construction

Chapter 1: Web Services Testing and soapui

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

Latest Trends in Testing. Ajay K Chhokra

An Introduction to text-based test automation and the TextTest tool

UML-based Test Generation and Execution

Outline. 1 Denitions. 2 Principles. 4 Implementation and Evaluation. 5 Debugging. 6 References

Introduction to unit testing with Java, Eclipse and Subversion

Example of Standard API

Author: Sascha Wolski Sebastian Hennebrueder Tutorials for Struts, EJB, xdoclet and eclipse.

Operating System Structure

Survey of Unit-Testing Frameworks. by John Szakmeister and Tim Woods

A Practical Guide to Test Case Types in Java

Magento Test Automation Framework User's Guide

The C Programming Language course syllabus associate level

XP and TDD. Extreme Programming and Test Driven Development. Bertrand Meyer, Manuel Oriol Andreas Leitner. Chair of Software Engineering ETH Zurich

CS3600 SYSTEMS AND NETWORKS

Automated Testing of Graphical Models in Heterogeneous Test Environments

AUTOMATING THE WEB APPLICATIONS USING THE SELENIUM RC

Tutorial IV: Unit Test

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Automatic promotion and versioning with Oracle Data Integrator 12c

Format string exploitation on windows Using Immunity Debugger / Python. By Abysssec Inc

Chapter 13: Program Development and Programming Languages

Test Automation Framework

How to Write a Simple Makefile

Unit testing with examples in C++

Continuous integration for databases using Redgate tools

Comp151. Definitions & Declarations

Software Quality Exercise 2

Testing Tools Content (Manual with Selenium) Levels of Testing

C++ INTERVIEW QUESTIONS

Informatica e Sistemi in Tempo Reale

Channel Access Client Programming. Andrew Johnson Computer Scientist, AES-SSG

Test case design techniques II: Blackbox testing CISS

Copyrighted , Address :- EH1-Infotech, SCF 69, Top Floor, Phase 3B-2, Sector 60, Mohali (Chandigarh),

Test Driven Development

Test-Driven Development as a Reliable Embedded Software Engineering Practice

BDD FOR AUTOMATING WEB APPLICATION TESTING. Stephen de Vries

Oracle Tuxedo Systems and Application Monitor (TSAM)

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

JUnit Automated Software Testing Framework. Jeff Offutt. SWE 437 George Mason University Thanks in part to Aynur Abdurazik. What is JUnit?

Agile Testing and Extreme Programming

IntroClassJava: A Benchmark of 297 Small and Buggy Java Programs

Xtreme RUP. Ne t BJECTIVES. Lightening Up the Rational Unified Process. 2/9/2001 Copyright 2001 Net Objectives 1. Agenda

How To Develop Software

Semantic Analysis: Types and Type Checking

Operating System Structures

Upping the game. Improving your software development process

Introduction. dnotify

Transcription:

Introduction to C Unit Testing (CUnit) Brian Nielsen Arne Skou {bnielsen ask}@cs.auc.dk

Unit Testing Code that isn t tested doesn t work Code that isn t regression tested suffers from code rot (breaks eventually) A unit testing framework enables efficient and effective unit & regression testing

What is unit testing? Unit testing Testing a unit of code, usually a class 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 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 isn t tested doesn t 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 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 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

Running automated tests Regression testing must be automated This requires they report pass/fail results in a standardized way Daily (Nightly) builds and testing Clean & check out latest build tree Run tests Put results on a web page & send mail (if tests fail)

Why formalize unit testing? Ad hoc manner Uncommenting or deleting test code / drivers printf Manual stimulation & observation Axiom: Code that isn t tested doesn t work If code has no automated test case written for it to prove that it works, it must be assumed not to work.

What is a testing framework? A test framework is a software tool for writing and running unit-tests provides reusable test functionality which: Is easier to use Is standardized Enables automatic execution for regression tests

What is a testing framework? A test framework is a software tool for writing and running unit-tests provides reusable test functionality which: Is easier to use Is standardized Enables automatic execution for regression tests

Why Unit-testing Framework A test framework is a software tool for writing and running unit-tests Most errors can be found by programmer Lightweight tool that uses the same language and development environment as the programmer Offers an easy, systematic, and comprehensive way of organizing and executing tests It is practical to collect and re-use test cases Automatic Regression Testing GUI-test case browser/runner Test report generation

CUnit Testing Each method is tested while developed Create tests first Start with simplest that works Incrementally add code while testing Tests serve as benchmark Optimize and refactorize without worry

Basic Use of FrameWork myunit.c myunittests.c cunit.lib C- compiler myunittests.exe Test-report.xml

Creating a Test Implement test functions Run the test using a TestRunner Group multiple TestCases using TestSuite

What is xunit? 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, A framework is a collection of classes, procedures, and macros

xunit principles 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: Lightweight software development methodology by programmers for programmers TDD (Test Driven Development) cycle 1. Write test case, and check it fails 2. Write the new code 3. Check that the test passes (and maybe refactor, re-test)

Core parts TestResult Collects results Test Suite TestRunner runs MyTest Test runner GUI runner Test Case TestFixture uses

Concepts Assertions 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

Test Case / suite A collection of concrete test methods A suite is a collection of test cases // 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 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)));

Test Cases Imp. void validclassification(){ CU_ASSERT_EQUAL(rectangularTriangle, classifytriangle(13,12,5) ); CU_ASSERT_EQUAL(scaleneTriangle, classifytriangle(15,10,5) );..

Driver File 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 Collection of test cases (or other test suites) in a logical unit Test Suites can be executed automatically

Test Reports 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

Test Runner XML file

Advice: xunit style Test cases exhibits isolation Sets up an independent environment / scenario and perform a distinct check One check per test method one assert per test method BUT consider amount of test code declarations to be written (when a assert fails the test method is stopped and no further asserts are checked). Test expected errors and exceptions

Advice: Application Design and program for testability Directly applicable to Pure function libraries API (With some footwork also user interfaces, network-, web-, and database applications)

Advice: Version Control Keep test code in a separate directory Keep both tests-sources and implemenation-source in version control Don t checkin unless version passes all tests

Conclusions Code that isn t tested doesn t work Code that isn t 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