1 / 131

Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer

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?.

royce
Download Presentation

Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Alcatel-Lucent CDCWorkshop, Coaching & Knowledge Transfer Unit Testing

  2. 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.

  3. What?

  4. 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…

  5. 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).

  6. 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”

  7. 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

  8. 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

  9. 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.

  10. A (very) simple example • 1 + 1 = 2 ?

  11. 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

  12. Assertion • Assert = Verify that the behavior is what you expect.

  13. Assertion • Assert class supplies methods to aid you in testing • Validates values • Compares values • Reacts upon exceptions

  14. 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))]

  15. 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…

  16. What kind of unit tests? • Typically, you have following kind of unit tests: • 1. (Strict) Unit tests • 2. Integration tests • 3. Acceptance tests

  17. What kind of unit tests? • (Strict) unit tests: • Tests “smallest testable part of an application”.

  18. What kind of unit tests? • Integration tests: • Tests more than 1 thing • E.g.: • Tests which uses multiple classes. • Tests that uses a database. • …

  19. 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

  20. Questions • Any questions about the “WHAT”?

  21. Unit Testing in .NET

  22. 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

  23. 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...

  24. NUnit • Framework for running .NET unit tests • Uses attributes to mark classes / methods: • [TestFixture] • [Test] • [SetUp] • [TearDown] • Has GUI application to run tests

  25. NUnit

  26. TestDriven.NET • AddIn for Visual Studio to run Nunit tests from within the IDE • Support for code coverage • Support for debugging tests

  27. With Team System • WHAT is Team System • HOW does it work • Unit Testing in Team System

  28. What

  29. What

  30. Unit Testing in Team System • Using the Test Manager • Select tests tobe run • Results are displayed in a graphicalway

  31. Unit Testing in Team System

  32. Code Coverage in Team System • Using the Test Manager • Select tests tobe run • Results are displayed in a graphicalway

  33. Code Coverage in Team System

  34. 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

  35. Questions • Any questions about “Unit testing in .NET”?

  36. Why?

  37. 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.

  38. The problem • Let’s convince you first WHY you have to write unit tests…

  39. Why is unit testing so important? • Prove that your code is doing what it is supposed to do.

  40. Why is unit testing so important? • Make sure you do not break other code when you develop a new functionality.

  41. Why is unit testing so important? • Make sure your code integrates with the code of your colleagues.

  42. Why is unit testing so important? • Make high quality code.

  43. Why is unit testing so important? • Reduce cost of bug fixing.

  44. Why is unit testing so important? • Sleep well at night, because you know your code is doing what it is supposed to do...

  45. How? Test driven development

  46. Test driven development • Let’s first compare traditional development with test driven development.

  47. Test driven development • Traditional development: • Write the code (“implementation”) • Test the code “manually” • Bug fixing

  48. 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

  49. 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

  50. 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.

More Related