Unit testing (eng)

35
Unit Testing 08/11/2011

description

● What is Unit Testing? ● Benefits ● What is Test Driven Development? ● What is Behavior Driven Development? ● Categories of (Unit) Tests / Software Testing Pyramid, Frameworks ● C++, Java, .NET, Perl, PHP frameworks ● Unit-testing Zend Framework application

Transcript of Unit testing (eng)

Page 1: Unit testing (eng)

Unit Testing08/11/2011

Page 2: Unit testing (eng)

Softjourn Inc.

Unit Testing

Anatoliy Okhotnikov

Softjourn Inc.

Page 3: Unit testing (eng)

Agenda

● What is Unit Testing?● Benefits● What is Test Driven Development?● What is Behavior Driven Development?● Categories of (Unit) Tests / Software Testing

Pyramid, Frameworks● C++, Java, .NET, Perl, PHP frameworks● Unit-testing Zend Framework application

Page 4: Unit testing (eng)

What is Unit Testing?● Ford new engine test area

● Special test harness that connects a gas and oil line

● For the output, a measuring device is connected to the drive shaft that comes directly out of the engine

● Engine starts and revs up

● Within about 5 minutes the computer is able to analyze the torque curve, gas usage, and oil usage

Page 5: Unit testing (eng)

What is Unit Testing?

● In computer programming, unit testing is a method by which individual units of source code are tested to determine if they are fit for use. A unit is the smallest testable part of an application. In procedural programming a unit may be an individual function or procedure. Unit tests are created by programmers or occasionally by white box testers.

● Ideally, each test case is independent from the others: substitutes like method stubs, mock objects, fakes and test harnesses can be used to assist testing a module in isolation. Unit tests are typically written and run by software developers to ensure that code meets its design and behaves as intended. Its implementation can vary from being very manual (pencil and paper) to being formalized as part of build automation

Page 6: Unit testing (eng)

Benefits

The goal of unit testing is to isolate each part of the program and show that the individual parts are correct. A unit test provides a strict, written contract that the piece of code must satisfy. As a result, it affords several benefits.

● Find problems early (in the development cycle)● Facilitates change (refactor, regression, etc.)● Simplifies integration (parts, sum & integration)● Documentation (live, understand API)● Design (TDD, no formal design, design element)

Page 7: Unit testing (eng)

What is Test Driven Development?● a software development process that

relies on the repetition of a very short development cycle: first the developer writes a failing automated test case that defines a desired improvement or new function, then produces code to pass that test and finally refactors the new code to acceptable standards.

● Kent Beck, who is credited with having developed or 'rediscovered' the technique, stated in 2003 that TDD encourages simple designs and inspires confidence.

Test

Code

Refactor

Page 8: Unit testing (eng)

What is Behavior Driven Development?● An agile software development technique that

encourages collaboration between developers, QA and non-technical or business participants in a software project.

● A response to Test Driven Development, including Acceptance Test or Customer Test Driven Development practices as found in Extreme Programming.

● It extends TDD by writing test cases in a natural language that non-programmers can read.

● With a Feature Injection, BDD covers the analysis space and provides a full treatment of the software lifecycle from vision through to code and release.

Page 9: Unit testing (eng)

Test Categories

Page 10: Unit testing (eng)

Categories of (Unit) Tests

● Small: Unit Tests● Check conditional logic in the code● A debugger should not be required in case of failure

● Medium: Functional Tests● Check whether the interfaces between classes

abide by their contracts

● Large: End-to-End Tests● Check for “wiring bugs”

Page 11: Unit testing (eng)

Software Testing Pyramid

Page 12: Unit testing (eng)

Frameworks

Wikipedia:

373 testing frameworks in

70 programming languages

C++ 51

C 39

JavaScript 29

Java 27

.NET 23

PHP 11

Common Lisp 11

Objective-C 9

Perl 9

ActionScript / Adobe Flex

9

Page 13: Unit testing (eng)

C++

● Differences in compilers, platforms, and programming styles. C++ is not exactly a clean, fully supported language, with one coding standard.

● List of features you want● Timing support: individual/total● Installation, Creation,

Modification, etc.

● CppUnit● Boost.Test● CppUnitLite● NanoCppUnit● Unit++● CxxTest● Unit Test++

Page 14: Unit testing (eng)

UnitTest++

● http://unittest-cpp.sourceforge.net/UnitTest++.html

● Lightweight unit testing framework for C++● Designed to do test-driven development on a

wide variety of platforms● Simplicity, portability, speed, and small footprint● ANSI portable C++: Win32, Linux, Mac OS X● Written and maintained by Charles Nicholson

and Noel Llopis “C++ For Game Programmers”

Page 15: Unit testing (eng)

UnitTest++ Example● a minimal C++ program to run a failing test

through UnitTest++

UnitTest++$ g++ -I./src -c test.cpp -o test.o

UnitTest++$ g++ test.o libUnitTest++.a -o test

UnitTest++$ ./test

test.cpp:6: error: Failure in FailSpectacularly: false

FAILURE: 1 out of 1 tests failed (1 failures).

Test time: 0.00 seconds.

Page 16: Unit testing (eng)

Java

● Java has an abundance of libraries that can help with the development of your tests cases

● Unit tests are fairly easy to write and have very rapid performance, so it's not going to take you long to run them.

● Don't Sacrifice Design for the Sake of Testing

● Use Statistical Testing for Non-deterministic Code

● JUnit● TestNG● JTest● Cactus● Mockito● JWalk● SureAssert

Page 17: Unit testing (eng)

JUnit

● http://junit.sourceforge.net/● JUnit is a simple framework to code repeatable

tests written by Erich Gamma and Kent Beck. It is used by the developer who implements unit tests in Java. Most famous of XUnits, JUnit is Open Source Software.

● Annotate a method with @org.junit.Test● When you want to check a value, import static

org.junit.Assert.*, call assertTrue() and pass a boolean that is true if the test succeeds

Page 18: Unit testing (eng)

JUnit Example● a minimal Java class to run a successful test

junit$ javac -cp junit-4.10.jar MyFirstTest.java

junit$ java -cp junit-4.10.jar:. org.junit.runner.JUnitCore MyFirstTest

JUnit version 4.10

Time: 0.004

OK (1 test)

Page 19: Unit testing (eng)

.NET

● There are a wide variety of unit testing tools available for .NET. Fortunately, the structure of unit tests is similar within most of the frameworks.

● NUnit has historically been one of the most popular frameworks

● There are 2 main parts of a unit testing system, the testing framework and a test runner.

● csUnit● MSTest● NUnit● NUnitAsp● .TEST● xUnit.net● Visual

Studio

Page 20: Unit testing (eng)

NUnit● http://www.nunit.org/ initially ported from JUnit● written entirely in C# and has been completely

redesigned to take advantage of many .NET language features, for example custom attributes and other reflection related capabilities.

● NUnit has two different ways to run your tests. The console runner, nunit-console.exe, is the fastest to launch, but is not interactive. The gui runner, nunit.exe, is a Windows Forms application that allows you to work selectively with your tests and provides graphical feedback

Page 21: Unit testing (eng)

NUnit Example● [TestFixture] is the

way to indicate that the class contains test code.

● [Test] is an indication that it is a test method.

● TransferFunds : expected <250> but was <150>

Page 22: Unit testing (eng)

Perl

● Put your tests in t/ and follow the convention of ##-name.t.

● Create a "Smoke Test" script and a bunch of generic tests

● Its as simple as deciding what you want to write, writing a test for what you are going to write, then writing the code that does it

● Start of by creating a file called tdd_is_cool.pm and a file called tdd_is_cool.t

● Test::More● TAP● Test::Harness● Test::Unit● Test::Class● Test::Builder● Test::Able

Page 23: Unit testing (eng)

Test::More

● http://search.cpan.org/perldoc?Test::More● Yet another framework for writing test scripts● By convention, each test is assigned a number

in order, often very useful to assign a name● Before anything else, you need a testing plan● use Test::More tests => 23;● ... run your tests ...● done_testing( $number_of_tests_run );

Page 24: Unit testing (eng)

Test::More Example

● A simple test module with two tests:●

● test::more$ perl tdd_is_cool.t● 1..2● ok 1 - use tdd_is_cool;● ok 2 - TDD is cool!

Page 25: Unit testing (eng)

PHP

● As unit testing has gained popularity, it has become a standard practice in PHP with libraries and frameworks such as Swiftmailer, the Zend Framework, Yii and Symfony all requiring unit test coverage of their source code

● Even good programmers make mistakes. The difference between a good and a bad programmer is that the good uses tests to detect his mistakes as soon as possible

● PHPUnit● SimpleTest● lime● Atoum● ojes● Testilence● Apache-Test● OnionTest

Page 26: Unit testing (eng)

PHPUnit

● PHPUnit is the de-facto standard for unit testing in PHP projects. It provides both a framework that makes the writing of tests easy as well as the functionality to easily run the tests and analyse their results.

● Part of xUnit family, created by Sebastian Bergmann, integrated & supported by Zend Studio/Framework.

● Simple installation with PEAR

Page 27: Unit testing (eng)

Installation

● PHPUnit should be installed using the PEAR Installer. This installer is the backbone of PEAR, which provides a distribution system for PHP packages, and is shipped with every release of PHP since version 4.3.0.

● The PEAR channel (pear.phpunit.de) that is used to distribute PHPUnit needs to be registered with the local PEAR environment. Furthermore, components that PHPUnit depends upon are hosted on additional PEAR channels.

● This has to be done only once. Now the PEAR Installer can be used to install packages from the PHPUnit channel:

pear channel-discover pear.phpunit.depear channel-discover components.ez.nopear channel-discover pear.symfony-project.com

● After the installation you can find the PHPUnit source files inside your local PEAR directory; the path is usually /usr/lib/php/PHPUnit.

pear install phpunit/PHPUnit

Page 28: Unit testing (eng)

Unit Testing Zend Framework● Let's create a new Zend Framework application:

zf create project zf-project

Page 29: Unit testing (eng)

First Run

● Launch an auto-generated unit tests from tests directory with the phpunit command:

● phpunit.xml – testing configuration settings● bootstrap.php – load application during testing

Page 30: Unit testing (eng)

IndexController Auto-Generated Test

Page 31: Unit testing (eng)

A Word of Explanation

● What are the parameters● URL generated from an Array of parameters● Are we dispatched to the correct place and result

● What we were checking in the test● dispatch – point the URL where we go to● assertModule – are we in the correct module?● assertController – checks the controller we are in● assertAction – checks the action● assertQueryContentContains – checks the content

Page 32: Unit testing (eng)

Coverage Report● Test coverage report(requires XDebug) with phpunit –coverage /report/directory/path

Page 33: Unit testing (eng)

Zend Testing Further Reading

● http://anton.shevchuk.name/php/unit-tests-zend-framework-application/

● You can use DOM assertions:● CSS selectors● XPath

● Use mock for Zend_Auth● Create emulators/wrappers● Write more tests!

Page 34: Unit testing (eng)

Recommendation Summary

● UnitTest++ advanced & convenient● JUnit the first and the best● NUnit most popular & rich● Test::More de-facto standard● PHPUnit (Zend_Test) must have

● C++● Java● .NET● Perl● PHP(Zend)

Even good programmers make mistakes.

The difference between a good and a bad programmer is that the good uses tests to detect his mistakes as soon as possible.

Page 35: Unit testing (eng)

Links● http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks

● http://gamesfromwithin.com/exploring-the-c-unit-testing-framework-jungle

● http://www.testdriven.com/

● http://www.phpunit.de/manual/3.6/en/index.html

● https://github.com/sebastianbergmann/phpunit/

● http://en.wikipedia.org/wiki/Unit_testing

● http://www.slideshare.net/sebastian_bergmann/getting-started-with-phpunit

● http://anton.shevchuk.name/php/unit-tests-zend-framework-application/

● http://www.slideshare.net/DragonBe/introduction-to-unit-testing-with-phpunit-presentation-705447