1.31k likes | 1.45k Views
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?.
E N D
Alcatel-Lucent CDCWorkshop, 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? • 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…
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).
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”
What is a unit test? • In human language: • A unit test = “a piece of software that testsanother 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
What is a unit test? • In human language: • A unit test = “a piece of software that testsanother 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
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.
A (very) simple example • 1 + 1 = 2 ?
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
Assertion • Assert = Verify that the behavior is what you expect.
Assertion • Assert class supplies methods to aid you in testing • Validates values • Compares values • Reacts upon exceptions
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))]
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…
What kind of unit tests? • Typically, you have following kind of unit tests: • 1. (Strict) Unit tests • 2. Integration tests • 3. Acceptance tests
What kind of unit tests? • (Strict) unit tests: • Tests “smallest testable part of an application”.
What kind of unit tests? • Integration tests: • Tests more than 1 thing • E.g.: • Tests which uses multiple classes. • Tests that uses a database. • …
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
Questions • Any questions about the “WHAT”?
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
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...
NUnit • Framework for running .NET unit tests • Uses attributes to mark classes / methods: • [TestFixture] • [Test] • [SetUp] • [TearDown] • Has GUI application to run tests
TestDriven.NET • AddIn for Visual Studio to run Nunit tests from within the IDE • Support for code coverage • Support for debugging tests
With Team System • WHAT is Team System • HOW does it work • Unit Testing in Team System
Unit Testing in Team System • Using the Test Manager • Select tests tobe run • Results are displayed in a graphicalway
Code Coverage in Team System • Using the Test Manager • Select tests tobe run • Results are displayed in a graphicalway
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
Questions • Any questions about “Unit testing in .NET”?
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.
The problem • Let’s convince you first WHY you have to write unit tests…
Why is unit testing so important? • Prove that your code is doing what it is supposed to do.
Why is unit testing so important? • Make sure you do not break other code when you develop a new functionality.
Why is unit testing so important? • Make sure your code integrates with the code of your colleagues.
Why is unit testing so important? • Make high quality code.
Why is unit testing so important? • Reduce cost of bug fixing.
Why is unit testing so important? • Sleep well at night, because you know your code is doing what it is supposed to do...
How? Test driven development
Test driven development • Let’s first compare traditional development with test driven development.
Test driven development • Traditional development: • Write the code (“implementation”) • Test the code “manually” • Bug fixing
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
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
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.