0% found this document useful (0 votes)
25 views41 pages

Lecture 3

Use different numbers for each input parameter.

Uploaded by

Nesrin Nesirova
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views41 pages

Lecture 3

Use different numbers for each input parameter.

Uploaded by

Nesrin Nesirova
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Unit Test Framework

1
Definition

• Test a software unit, the most little.


àIn C the function, java : method.

• To create an unit test you need to write code, it is


called a test harness. The necessary code may be
important.

• Unit test framework make it more easy and less


costly.

2
definition

A • If you want to test


function C your test
B C
harness need to call
function C and mock
function D and E.
D E

3
Sample

• Here the function:

• Which tests would you write to test it?


• By the way, it is a good idea write unit test by
reading the code?

4
Unit Test methodologies
• Defined within agile approach like XP:
– Write the test before coding.
– TDD

• Defined by standard like DO-178B (aeronautic


industry)
– Write the test after coding or at same time and
check with code coverage efficiency of unit test.

5
Unit test methodology :Test Driven Development

Advantages:
• Specify before coding thanks unit test.
• Get testable code.
• Refactor code.
• Get trust in your code.
• Get clean and simple code.
TDD

• Test First is a development practice


introduced by eXtreme Programming (XP)

Develop a test script


visual fonction/method usage
Run test that fails
Create the initial function/method and
nothing else (minimum)
Run test that should success now

Add a new test


Run test that fails
Modify function/method so that it meets
requirement implemented by test
Run test that should success now

TDD
PROs
– Code only what you need (XP)
– Unit tests describe what the component achieve
in term of functionalies
– Unit test could be used as component
documentation
CONST
- Design may need to evoluate when you add new
tests
8
Unit test and standard

• Aeronautic industry:
– Requirement based testing
– Use of test techniques is MANDATORY
• Equivalent classes combined with limit values testing
• Decision table
– Depending on criticality level a specific code
coverage level will be required.
– Depending on criticality level independence may
be required (developer won’t test his own code)

9
Unit test and standard
Low Level
Requirement

Test Design Code Design

Development of
Test Writing
component

And maybe
Test execution
Bugs
10
UNIT TEST METHODOLOGY
• With TDD the test helps you to develop.
• With standard the test is used to criticize your
code.

• Agile approach has a more « positive » use of


the test.

11
Unit test

– Set UP: set up of test environment (ex mocks


…)
– Exercice : make an action (call a method)
– Verify: control the result of the action
– Teardown : clean and extra check
• 3As
– Arrange (set up)
– Act (exercice)
– Assert (verify)
Set UP

• What should I do before doing unit test on


function computePrice:

13
Exercise/Assert
• Assertion are used to make comparison.
• Depending on the result the status of the test
will be:
– FAIL
– PASS
There are specific assertion depending of the
type of object you want to compare.

14
SAMPLE

Here you compare two floating object. As float are not


exactly represented you need to add a epsilon (third
parameter).

15
Specific assertion
• You have specific framework like dbunit that
helps you to compare table for example:

16
Tear Down
• Mainly used for:
– Clean the test environment (often done also in set
up).
– Do extra checks

17
Set Up/ Tear Down
• Avoid duplication of code.
• Test is more clear.
• You can also have set up and tear down for a
set of tests:

Why are the


method static?

18
Unit test

• In a test suite each unit should be


independent (no order).
• This can be achieved by:
à SetUp
à Tear Down
à mock instead of real method
If you don’t use a mock but call
another method what type of test are
you doing?
Refactoring

If you want to refactor yours test you need unit test :


Advantages of refactoring:
• Reduce code complexity
• Reduce coupling
• Make code more readable
• Make it easy for future evolution
• Reduce maintainability cost
• Use of Design Patterns
When refactoring :
• In case of code duplication
• Too long method/function
• Method/function that have several responsibilities
• …
Pre-Requisite
• Before going to integration test, components
have to be tested before.
• Why ?

21
CI
• Unit test are integrated in CI to
– Get visibility
– Gather results
– Get an overview on component quality
– Be used as a criteria for going to integration test

22
SONAR DASHBOARD
• Unit Test results can also be integrated in a
« quality dasboard », like sonar dashboard:

23
Unit Test : technologies

• Frameworks : Xunit
• Junit pour java
• PhpUnit pour php
• GoogleTest, cppUnit pour C++
• NUNIT pour C#
• …

• Offer specific tools for unit testing:


– Set Up and TearDown
– Assertion
– Result formatting
Unit Test framework:

• Cmocka
– Unit test framework offering also the feature of
mocking(Mock).
• Multi platform + embedded software
• Features
– Standard Assertion
– Fixtures ( setUp and TearDown)
– Mock (call order, parameter check)
– Different result output
Unit Test sample

Assertion

Adding test to test suite

XML formatted result

Group test is executed with a


Toto is the name of testsuite set Up and a teardown

26
Unit: How to

1. Creation of one or several testcase as


function
2. Add the function in a table
3. Run the tests contained in table

27
Tests Unitaires : assertions

https://api.cmocka.org/group__cmocka__asserts.html

28
Unit test: macros

https://api.cmocka.org/group__cmocka__exec.html

There are other macros but there are deprecated.

29
Macros: Use

• The macros c_unit_test_* allows you to add


testcase with or without setUp procedure.

• SetupTest1 will be executed once before test1.

30
Unit Tests : execution

https://api.cmocka.org/group__cmocka__exec.html

Setup and teardown are executed at beginning and end


of test suites.
à Common actions used by all the tests in the group.

31
Cmocka: skip and fail

• Fail:

• Skip

• How can fail and skip be used?

32
Unit Test: result

Environment variable :CMOCKA_MESSAGE_OUTPUT


– STDOUT
– SUBUNIT
– TAP
– XML (junit)
Environment variable : CMOCKA_XML_FILE contains link to result
file.

33
Test report

34
Etat du test

• Succeeded
• Test has succeeded
• Test contains bugs

• Failed
• Test has failed
• Test contains bugs
à You have to test your tests
? Any idea how to do
One Sample

• A function that concatenates 2 characters


string:

36
One Sample

• SetUp

37
Two tests

38
Main

39
Writing unit test : good practices

• Test allows to evaluate codeà confidence

• Unit test should be independant à slice problems

• Write assertion firstà you’ll write the rest of the test


more easely

• Data: Use real life data (production, field)

• Pay attention to data inversion

• For expected result write the computation : 2*4


instead of 8.
Data Inversion

Example : 2 similar input parameters


like :
– Personal phone number
– Professional phone number

• If you use same number in both


inputs and there is an issue in the
code (use mismatch of both number
) your test won’t detect it.

You might also like