alcatel-lucent cdc workshop, coaching & knowledge transfer
DESCRIPTION
Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer. Unit Testing. Introduction. Let’s find out WHAT a unit test is? Convince you WHY unit testing is so important. Showing you HOW you should create unit tests. Best practices . What?. What?. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/1.jpg)
Alcatel-Lucent CDCWorkshop, Coaching & Knowledge Transfer
Unit Testing
![Page 2: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/2.jpg)
Introduction
Let’s find out WHAT a unit test is?
Convince you WHY unit testing is so important.
Showing you HOW you should create unit tests.
Best practices.
![Page 3: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/3.jpg)
What?
![Page 4: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/4.jpg)
What?
Let’s find out first what a “unit test” is. Let’s define a clear definition, so that we all
are talking about the same thing. A gentle introduction to unit testing…
![Page 5: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/5.jpg)
What is a unit?
Let’s first define what a “unit” is. A narrow definition of “unit”: A “unit” is the
“smallest testable part of an application”. In Object Oriented design, the smallest unit is
a “Class” (at first approximation).
![Page 6: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/6.jpg)
What is a test?
Let’s also define what a “test” is. A definition of “test”:
A “test” is a method to verify or falsify an expectation with an observation.
A definition by wikipedia:A unit test = “a procedure used to validate that individual modules or units of source code are working properly”
![Page 7: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/7.jpg)
What is a unit test?
In human language: A unit test = “a piece of software that tests
another piece of software and tells you whether the tested piece of software is working correctly”.
Let’s make sure our code is doing what it is supposed to do……by testing it
![Page 8: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/8.jpg)
What is a unit test?
In human language: A unit test = “a piece of software that tests
another piece of software and tells you whether the tested piece of software is working correctly”.
Let’s make sure our code is doing what it is supposed to do……by testing it
![Page 9: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/9.jpg)
Let’s ask Martin Fowler?
http://www.artima.com/intv/testdriven4.html“A conversation with Martin Fowler”
Bill Venners: What is the unit in unit test? Define unit test.
Martin Fowler: That's very difficult. To a first approximation, it's a class. But as you work with unit tests more, you begin to realize you're testing little areas of responsibility, and that could be a part of a class or it could be several classes together. I don't get too hung up about it, but I'd say if you're just starting out, think of unit tests as just writing a test case per class.
![Page 10: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/10.jpg)
A (very) simple example
1 + 1 = 2 ?
![Page 11: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/11.jpg)
Parts of a unit test
SetUp Preparation phase, where state can be initialized,
data made ready, ... Test
Execution of check logic to compare expected and received results
Teardown Restoration of the state to the previous state
before the test was run
![Page 12: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/12.jpg)
Assertion
Assert = Verify that the behavior is what you expect.
![Page 13: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/13.jpg)
Assertion
Assert class supplies methods to aid you in testing
Validates values Compares values Reacts upon exceptions
![Page 14: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/14.jpg)
Assertion
Assert.AreEqual(expected, actual) Assert.AreNotEqual(expected, actual) Assert.Fail() Assert.Greater(value, value) Assert.Less(value, value) Assert.IsNull(value) Assert.IsNotNull(value) [ExpectedException(typeof(Exception))]
![Page 15: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/15.jpg)
What kind of unit tests?
Strictly, a “unit test” tests a “unit”, which is – as seen in the definition – the “smallest testable part of an application”.
However, you would probably like to test a functionality that uses more than one class…
![Page 16: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/16.jpg)
What kind of unit tests?
Typically, you have following kind of unit tests: 1. (Strict) Unit tests 2. Integration tests 3. Acceptance tests
![Page 17: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/17.jpg)
What kind of unit tests?
(Strict) unit tests: Tests “smallest testable part of an application”.
![Page 18: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/18.jpg)
What kind of unit tests?
Integration tests: Tests more than 1 thing E.g.:
Tests which uses multiple classes. Tests that uses a database. …
![Page 19: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/19.jpg)
What kind of unit tests?
Acceptance unit tests: Necessary to prove that a certain required business rule
is working correctly. Functional nature. Probably uses more “units”. Not necessarily written up-front… According to the analysis document:
Pre conditions Post conditions Normal flow Alternate flows
![Page 20: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/20.jpg)
Questions
Any questions about the “WHAT”?
![Page 21: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/21.jpg)
Unit Testing in .NET
![Page 22: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/22.jpg)
xUnit frameworks
Original framework was created for SmallTalk Ported to various languages where ‘X’ stands
for the language JUnit (java), Nunit (.net), CppUnit (c++), VBUnit
(visual basic), RUnit (ruby), PyUnit (python), ... Standard test architecture NUnit: standard for .NET
![Page 23: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/23.jpg)
Unit Testing in .NET
With NUnit / TestDriven.NET With Team System
Whatever unit testing framework you are using does not matter, the principles are the same...
![Page 24: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/24.jpg)
NUnit
Framework for running .NET unit tests Uses attributes to mark classes / methods:
[TestFixture] [Test] [SetUp] [TearDown]
Has GUI application to run tests
![Page 25: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/25.jpg)
NUnit
![Page 26: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/26.jpg)
TestDriven.NET
AddIn for Visual Studio to run Nunit tests from within the IDE
Support for code coverage Support for debugging tests
![Page 27: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/27.jpg)
With Team System
WHAT is Team System HOW does it work Unit Testing in Team System
![Page 28: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/28.jpg)
What
![Page 29: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/29.jpg)
What
![Page 30: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/30.jpg)
Unit Testing in Team System
Using the Test Manager Select tests to be run Results are displayed in a graphical way
![Page 31: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/31.jpg)
Unit Testing in Team System
![Page 32: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/32.jpg)
Code Coverage in Team System
Using the Test Manager Select tests to be run Results are displayed in a graphical way
![Page 33: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/33.jpg)
Code Coverage in Team System
![Page 34: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/34.jpg)
A simple exercise
Visual Studio Solution: ItemSolutions.Calculator
The calculator can sum 2 numbers
There should be a test to prove this functionality is working.
Make a test that proves that 2 + 2 = 4
![Page 35: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/35.jpg)
Questions
Any questions about “Unit testing in .NET”?
![Page 36: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/36.jpg)
Why?
![Page 37: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/37.jpg)
The problem
Unit tests don’t get written, because: Most developers know that they “should” write unit tests,
but they think it is cumbersome and it takes more time. So, they don’t...
Most unit tests are written after the business logic is written. Writing a test afterwards is difficult. Therefore, you can imagine that most tests don’t get written at all.
Testing of code is usually done during user testing.
Result: unit tests don’t get written at all.
![Page 38: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/38.jpg)
The problem
Let’s convince you first WHY you have to write unit tests…
![Page 39: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/39.jpg)
Why is unit testing so important?
Prove that your code is doing what it is supposed to do.
![Page 40: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/40.jpg)
Why is unit testing so important?
Make sure you do not break other code when you develop a new functionality.
![Page 41: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/41.jpg)
Why is unit testing so important?
Make sure your code integrates with the code of your colleagues.
![Page 42: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/42.jpg)
Why is unit testing so important?
Make high quality code.
![Page 43: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/43.jpg)
Why is unit testing so important?
Reduce cost of bug fixing.
![Page 44: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/44.jpg)
Why is unit testing so important?
Sleep well at night, because you know your code is doing what it is supposed to do...
![Page 45: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/45.jpg)
How?Test driven development
![Page 46: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/46.jpg)
Test driven development
Let’s first compare traditional development with test driven development.
![Page 47: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/47.jpg)
Test driven development
Traditional development: Write the code (“implementation”) Test the code “manually” Bug fixing
![Page 48: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/48.jpg)
Test driven development
Test driven development (TDD): 1. Write a test first 2. Write the least code you need in order to compile 3. Run the test: it will fail (otherwise, the test is not good) 4. Implement the least code to pass the test 5. Run the test again:
If it fails, go back to 4 If it passes, start again with step 1
![Page 49: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/49.jpg)
The Ugly Duckling
Following statements are mainly based on the document “The Ugly Duckling” from Martin Fowler. An article written in 1998 (!), which explains “testing
methods” and notices that it is considered as “the ugly duckling”.
http://www.martinfowler.com/distributedComputing/duckling.pdf
![Page 50: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/50.jpg)
Testing methods in practice
Self-testing code Write code that tests your code. Testing is not something to start after you have finished
coding. Test code is as important a deliverable as production code
and should be given the same emphasis.
![Page 51: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/51.jpg)
Testing methods in practice
Incremental development Every time I try to add new features to software, I
stop and ask myself: “What is the smallest piece of new function I can add?”
I then focus on adding that feature alone, and I do not move to a new feature until the feature is complete - including the self-testing code for that feature.
Development then proceeds by small steps, with the testing code and the production code proceeding in tandem.
![Page 52: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/52.jpg)
Testing methods in practice
Write test code before production code It helps to focus on exactly what this incremental
step involves. Helps you concentrate on the interface for the
new feature rather than the implementation. You are asking yourself: “How will a client use this
new feature?”
![Page 53: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/53.jpg)
Testing methods in practice
Simple indication of test results The tests must give a simple indication of whether
they pass or fail. “OK” “Here is a list of failures”
![Page 54: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/54.jpg)
Testing methods in practice
Quick feedback If in the future you do something that breaks
some other part of the system, the tests will quickly tell you.
You’ll know it must be something you just did. Quickly find the bug vs. hours of bug chasing.
![Page 55: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/55.jpg)
Testing methods in practice
Continuous integration “It amazes me when a programmer checks in code
after days of work and just assumes that it will work with whatever anyone else has checked in.”
Integrate a lot: check in a lot and make regular builds.
Continuous integration makes bugs show up early.
![Page 56: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/56.jpg)
Testing methods in practice
“Well this sounds reasonable, but I’ve got deadlines to make” The crucial realization is that self-testing code
actually speeds up writing code. It does this because it makes debugging much shorter.
In traditional development: much more time and effort is spent in removing bugs than writing code.
Less time debugging, thus developing faster.
![Page 57: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/57.jpg)
Testing methods in practice
A unit test runs without intervention No intervention should be required to run a unit test. Anyone should be able to run it. A unit test must be able to run in an automated process
(build).
![Page 58: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/58.jpg)
A simple exercise
Visual Studio Solution: ItemSolutions.Calculator
Let’s go back to the calculator again and implement a multiplier…
Try to prove that 2 x 2 = 4 Write tests first!!!
![Page 59: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/59.jpg)
Questions
Any questions about the “HOW”?
![Page 60: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/60.jpg)
Quality
![Page 61: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/61.jpg)
Quality of tests
Quality of code is crucial Quality of unit tests is even more important It’s better to have no tests, than bad tests
![Page 62: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/62.jpg)
It’s all about quality…
Good unit tests allow us to produce high quality software…
![Page 63: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/63.jpg)
It’s all about quality…
…but how do we follow-up? Automated build system Test reports Code coverage …
![Page 64: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/64.jpg)
Automated build system
Integrate often: check in frequently An automated build system should make a
build as frequent as possible (at least once a day, “daily build” or “nightly build”).
All unit tests should be run on the production code.
Automated feedback on the unit tests should be given.
![Page 65: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/65.jpg)
Test report
Used in an automated build system Feedback on unit tests Results are displayed in a graphical, easy to
understand report Also valuable for management
![Page 66: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/66.jpg)
Test report
![Page 67: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/67.jpg)
Test report
![Page 68: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/68.jpg)
Code Coverage
Calculates coverage of unit testing Does not monitor quality of unit tests Does monitor what code is tested / untested Results can be displayed in a graphical, easy to
understand report Also valuable for management You should set a minimum code coverage percentage
per project. Be careful with this figure, it can be a misleading
indicator...
![Page 69: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/69.jpg)
Code Coverage
![Page 70: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/70.jpg)
Quality control
How can we check that our tests are good? Replace some functionality, by some other but
broken functionality Run the tests again If all tests succeed then:
There might be a test missing Or: There might be a bad test (should fail with the
broken functionality)
![Page 71: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/71.jpg)
Automatic quality control
Use a code mutation framework Mutates some code and run the tests Both for java and .NET Jester: JUnit test tester Nester: NUnit test tester
![Page 72: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/72.jpg)
A simple exercise
Visual Studio Solution: ItemSolutions.Calculator
Let’s check the quality of our tests
Replace the sum method by the multiply method and vice versa
If any tests succeeds, the quality of the tests is poor
![Page 73: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/73.jpg)
A simple exercise
Change the tests so quality can be guaranteed
![Page 74: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/74.jpg)
Questions
Any questions about “QUALITY”?
![Page 75: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/75.jpg)
Applied on the work floor
![Page 76: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/76.jpg)
Applied on the work floor
Let’s take a look how test driven development can be applied in a real environment...
![Page 77: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/77.jpg)
How
Automated build system: continuous integration cycle
Agile device: lavalamp Project dashboards for each project Reports
Unit test report Code coverage report Functional coverage report
![Page 78: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/78.jpg)
How
![Page 79: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/79.jpg)
Continuous integration
Automated build triggered by changes in code repository
Fast feedback Using cruise control Standard ANT / NANT scripts
Get latest code Build it Test it Give
feedback
![Page 80: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/80.jpg)
Agile device: lavalamp
Shows status on all projects Build failure Test results (success / failure / ignored) Coverage results (percentage)
![Page 81: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/81.jpg)
Project dashboards
Graphical approach for all projects Project information is accessible to
everybody, from anywhere and at anytime Professional look on projects for customers
![Page 82: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/82.jpg)
Project dashboards
![Page 83: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/83.jpg)
Project dashboards
![Page 84: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/84.jpg)
Questions
Any questions about “Applied on the work floor”?
![Page 85: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/85.jpg)
Best practices
![Page 86: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/86.jpg)
Best practices
Purpose WHAT are the best practices HOW can we implement them What is GOOD and what is BAD Does this impact on the DESIGN EXAMPLE driven
![Page 87: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/87.jpg)
Purpose of best practices
Create general conventions Ensure quality of code but also quality of tests Its better to have no tests than bad tests Tests should be maintainable Tests should be clear to everybody, not a pain
in the ass
![Page 88: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/88.jpg)
Best practicesPC-COF
![Page 89: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/89.jpg)
PC-COF
Tests should comply to the PC-COF* rules: Partial runs are possible Consistent pass / fail result Configuration is not needed Order does not matter Fast
* Roy Osherove: www.iserializable.com
![Page 90: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/90.jpg)
Partial runs are possible
Tests should be executable without depending on other services, ...
![Page 91: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/91.jpg)
Consistent pass / fail
Tests should always yield the same results Tests should pass and fail in a predictable
manner
![Page 92: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/92.jpg)
Configuration is not needed
Tests should run without configuration This is very hard to accomplish:
Database connection settings Authentication / Authorization Assembly dependency ...
![Page 93: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/93.jpg)
Order does not matter
The order in which tests are executed should not matter for consistent results
![Page 94: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/94.jpg)
Order does not matter: sample
Problem: Insert person in a database Delete that person from the database Should the delete come before the insert or the
insert come before the delete? Solution:
Rollback database state after each test. Each test should be self reliant and should not use
data from other tests
![Page 95: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/95.jpg)
Fast
Tests should run fast to allow people to run them often
If tests are slow, they will never be run and results will be poor
![Page 96: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/96.jpg)
PC-COF: practical
Problem: When a client opens a new bank account, its social
security number needs to be validated against an authorized authority.
Link this problem to the PC-COF rule and explain
How can we solve the problem?
![Page 97: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/97.jpg)
PC-COF: practical
Solution: Extract an interface of the service (IValidator) Create a dummy validator class implementing the
interface, a stub Add a constructor to control the result (true /
false) Replace the authorized authority by the dummy
validator
![Page 98: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/98.jpg)
PC-COF: practical
Can you think of any other functionality which might be hard to test?
![Page 99: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/99.jpg)
PC-COF: practical
Problems: Communication system Workflow system Database application
![Page 100: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/100.jpg)
Best PracticesWhat should you test
![Page 101: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/101.jpg)
What should be tested
All code that performs some logic All code that is susceptible to change All code that may be susceptible to bugs All code that is difficult All code that will need refactoring All code that will be the core of the system
![Page 102: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/102.jpg)
What shouldn’t be tested
Typical entity methods: constructors, getters, setters that do not contain any logic
Temporary helpers only used for tests Real interfaces, web services, external systems
(use stubs / interfaces)
![Page 103: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/103.jpg)
Test one thing at a time
Test only things related to the test you are writing
Small problems are easier to test Large problems are easy to get stuck upon Large wholes make tests hard to read and
understand, slow and unlikely to be changed when failed
![Page 104: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/104.jpg)
Test one thing at a time: example
Create new person and save it to the database Later changes (validation / authorization)
shouldn’t affect this test Validate person before saving Authorize user before saving
![Page 105: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/105.jpg)
Acceptance tests
This kind of tests is used to test the complete flow of one part of the application
Create new person and save it to the database while validating the person and authorizing the user
![Page 106: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/106.jpg)
Best PracticesDesign
![Page 107: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/107.jpg)
Design
Test Driven Development may have implications on the Design of an architecture
Design should be testable, so architecture may change to allow to be tested
A testable design: Avoid singletons Use interfaces Avoid GOD methods Use factory pattern Single responsibility for classes and methods
![Page 108: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/108.jpg)
Best PracticesNaming
![Page 109: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/109.jpg)
Naming conventions
Names of tests should be clear at first sight Everybody should understand the purpose
without reading the test Behaviour of tests should be clear Test name should express a specific
requirement Test name should include the expected input
or state and the expected result
MethodName_Behaviour_ExpectedResult
![Page 110: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/110.jpg)
Naming conventions: problem
Public int Sum(params int[] values) Numbers larger than 1000 that are passed in
should not be considered (becomes 0)
![Page 111: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/111.jpg)
Naming conventions: solution
TestSum_NumberBiggerThan1000
TestSum_NumberIsIgnored
TestSum_NumberIgnoredIfBiggerThan1000
TestSum_NumberIgnoredIfBiggerThan1000_NumberIsIgnored
![Page 112: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/112.jpg)
Naming conventions: example
When a user is created without a username (null), an exception should be thrown
TestCreateUserWithNullUserName
TestCreateUser_UsernameIsNull_ThrowsInvalidOperationException
![Page 113: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/113.jpg)
Naming conventions @ KBC
The naming conventions are used in a slightly different way at KBC
Unit Tests:Class_Method_Behavior_ExpectedResult
Scenario Tests:Class_Step_Action_Behavior_ExpectedResult
![Page 114: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/114.jpg)
Best PracticesBugfixing
![Page 115: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/115.jpg)
Bugfixing
Write a test when a bug is found Cover the bug in a test so it is reproducable Fix the bug Run the tests to make sure the bug is fixed and it
cannot re-enter the system again Result:
Certainty that you didn’t break other code you didn’t think of or from another developer
Quality and bugfree software No more sleepless nights
![Page 116: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/116.jpg)
Bugfixing: practical
Visual Studio Solution:ItemSolutions.BugFixing
Code to manage bar expenses / income Price of drinks should be fixed Between 16h and 18h it’s happy hour: buy 2
drinks, pay only 1 The functionality is only working from 16h till 17h
Write tests to prove this bug and to fix this bug
![Page 117: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/117.jpg)
Best PracticesOrder of statements
![Page 118: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/118.jpg)
Order of statements
Order of statements and Asserts can decide upon the quality of your tests
You have to maintain a consistent state while running tests
Data can remain in the database when statements are executed in the wrong order
Asserts throw exceptions !!! when they fail The AutoRollbackTest covers this problem
![Page 119: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/119.jpg)
Order of statements: problem
Delete new record
Assert on new values
Insert new record
![Page 120: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/120.jpg)
Order of statements: solution
Assert on new values
Delete new record
Insert new record
![Page 121: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/121.jpg)
Best PracticesDatabase testing
![Page 122: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/122.jpg)
Database testing
According to the PC-COF, this is bad External system Susceptible to failure
Inevitable and needed Use transaction base testing by using Auto
Rollback tests Open connection and transaction in SetUp Rollback transaction and close connection in TearDown
Data will never remain in the database!
![Page 123: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/123.jpg)
Best PracticesDates
![Page 124: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/124.jpg)
Dates
Fixed dates When working with pre-defined dates, fixed dates
can be used in tests Calculations for fixed holidays, leap-years, ...
Use new DateTime to create the fixed date new DateTime(2007, 05, 07)
![Page 125: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/125.jpg)
Dates
Variable dates (timespans) When working with variable dates, DateTime.Now
should be used Age calculations / assertions, timespan calculations,
calculations against the current system date Without using DateTime.Now, consistent results
cannot be ensured Use current system date and add a number of
days / years / ... instead DateTime.Now.AddDays(5) DateTime.Now.AddYears(-5)
![Page 126: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/126.jpg)
Dates: example
NumberOfDaysLeft = Current Date – Due Date (= calculating a timespan)
Use fixed date in test: test result will not be consistent and will change throughout time
Hard to make some consistent assertions Use DateTime.Now in test: test result will be
consistent
![Page 127: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/127.jpg)
Best PracticesTest data factories
![Page 128: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/128.jpg)
Test data factories
Some tests have to prepare lots of data in the SetUp
To avoid code duplication, a separate TestFactory class can be used
This factory can prepare the data so it is reusable
![Page 129: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/129.jpg)
Test data factories: example
To test if holidays can be assigned to an employee: An employee should be created Days should be assigned to this employee Holidays should be assigned
If we put this in the SetUp: Code cannot be reused SetUp is large, hard to maintain and read
![Page 130: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/130.jpg)
Questions
Any questions about the “Best Practices”?
![Page 131: Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer](https://reader036.vdocuments.us/reader036/viewer/2022062411/56816697550346895dda7f28/html5/thumbnails/131.jpg)
Contact
Item SolutionsPlantin en Moretuslei 1552140 Borgerhout +32 3 236 64 03 [email protected]
SpeakerKristof Rennen [email protected]