Lecture 3
Lecture 3
1
Definition
2
definition
3
Sample
4
Unit Test methodologies
• Defined within agile approach like XP:
– Write the test before coding.
– TDD
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
• 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
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.
11
Unit test
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
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:
18
Unit test
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#
• …
• 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
26
Unit: How to
27
Tests Unitaires : assertions
https://api.cmocka.org/group__cmocka__asserts.html
28
Unit test: macros
https://api.cmocka.org/group__cmocka__exec.html
29
Macros: Use
30
Unit Tests : execution
https://api.cmocka.org/group__cmocka__exec.html
31
Cmocka: skip and fail
• Fail:
• Skip
32
Unit Test: result
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
36
One Sample
• SetUp
37
Two tests
38
Main
39
Writing unit test : good practices