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?.
01 / 131Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer
1 / 17
Télécharger la présentation
Alcatel-Lucent CDC Workshop, Coaching & Knowledge Transfer
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation
Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement.Téléchargez la présentation en cliquant sur ce lien.Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.
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.
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
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
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
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…
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.
Connexion au compte
Connectez-vous pour publier votre contenu sur SlideServe.