Unit Testing Maintaining Quality. How do you test? Testing to date…

22
Unit Testing Maintaining Quality

Transcript of Unit Testing Maintaining Quality. How do you test? Testing to date…

Unit TestingMaintaining Quality

How do you test?

• Testing to date…

Testing Large Systems

• Old school:– Programmers write parts of code• Maybe check tricky bits

– Someone tries to build whole project• Sort out incompatibilities

– Hand it off to testing• This job is awful

Testing Goals

• Principles:– Tests should cover as much code as possible– Tests should be run after any significant change– Tests should not clutter up core source

Testing Goals

• Principles:– Tests should cover as much code as possible– Tests should be run after any significant change– Tests should not clutter up core source

• So:– Tests better be easy to write– Tests better be easy to run– Tests better exist outside normal structure

Unit Tests

• Unit Test : – Tests one module of code– Automated– Run after any changes to code

Other Testing

• Still need:– Integration testing• But easier

– UI testing• But just testing UI code

Doing Unit Testing

• Basic idea:– Write functions TESTS to test your code• Tests can be passed or failed

What does it actually look like?

• Base project– Normal application– Header file(s) for classes– .cpp file(s) for classes– main file with "real" program

What does it actually look like?

• Test project– Special header with test stuff• Included in test files only

– One .cpp file to test each class – New main file to run tests

Building / Running

• Activate / Build / Run standard projectNormal program

Building / Running

• Activate / Build / Run test projectRun unit tests

Details

• catch.hpp

Don't touch

Details

• mainTester.cpp– Has main function• Runs tests automatically

– Can be configured

Details

• CLASSXTESTER.cpp– Includes class.h / catch.hpp– Has TEST_CASEs

Expression that must be true

Machine name - must be unique Description

Details

• CLASSXTESTER.cpp– Includes class.h / catch.hpp– Has TEST_CASEs

Expression that must be true

Machine name - must be unique Description

== doubles

• Not OK to compare doubles with ==

== doubles

• Can write your own function to test for approximate equality:

== doubles

• But Catch includes macro to simplify:– Approx( value )

Details

• Pattern– Make an object– Call functions to be tested– REQUIRE correct data• Must use public methods to access

What Do I Test

• Test public interface• Test every method/construct except…– Trivial one liners:• Usually don't directly test getXXX()

– IO code:• Hard to do from testing frameworks

Features Test Mapping

• Focus on one features per test• Some features may need multiple tests:connect.setURL("http://www.google.com") connect.setURL("www.google.com") connect.setURL("ftp://chemeketa.edu")

isPrime(3) isPrime(4)