460 likes | 796 Views
Test - DRIVEN DEVELOPMENT. Lecture 3. Definition. Test-driven development (development through testing) is a technique of programming, in which the unit tests for the program or its fragment are written before the program and, in essence, manage its development. Methodology :
E N D
Test-DRIVEN DEVELOPMENT Lecture 3
Definition • Test-driven development (development through testing) is a technique of programming, in which the unit tests for the program or its fragment are written before the program and, in essence, manage its development. • Methodology: • Write a new code only when automatic test doesn’t work. • Remove duplication
Development cycle (briefly) • Red - write a short test that does not work and may not even compile. • Green- make test work as quickly as possible, do not think about the correctness of design and clean code. Write just enough code to make the test working. • Refactoring- delete any duplication from the written code. • Red- green - refactoring. • Refactoring is the process of full or partial conversion of the internal structure of the program while maintaining its external behavior.
Development cycle • The programming system is extracted from the repository if it is in a consistent state, when the full set of unit tests run successfully. • Add a new test. It may consist of verifying whether the system implements a new behavior or contains some error, which is become known recently. • The full set of tests is performed successfully except a new test which fails. This step is necessary to verify the actual test - if it is included in the general system of testing and if it correctly reflects the new requirement to the system, which, of course, does not satisfy to yet. • The program will change to ensure that all tests run as quick as possible. You need to add the simplest solution that meets the new test, and at the same time do not spoil the existing tests. A large part of the undesirable side effects and remote effects from the changes is made at this stage, using a full set of tests.
Development cycle (continuation) • The full set of tests runs successfully. • Now, when the required functionality is achieved in the simplest way, the program is refactored to improve the structure and eliminate redundant, duplicated code. • The full set of tests runs successfully. • The set of changes made in this cycle in tests and program is saved in the repository. Now again, the program is in a consistent state and contains a clear improvement over the previous state. The duration of each cycle is a few minutes.
Example 1. Fibonacci Numbers • The Fibonacci sequence is determined by the following relation:F(n) = F(n - 1) + F(n - 2)F(1) = 1F(0) = 0 • You want to write a function to determine the n-thnumber of the sequence.
Example 1 • The first test: • The implementation of function:
Example 1 • Check the second number of the sequence
Example 1 • Modify the function
Example 1 • Add new test, checking the 3rd and the 4th numbers of the sequence:
Example 1 • Modify the function
Example 2. Function CombinePaths • We want to implement the function of addition two given path. • C:\Data\+MySQL\data.sql=C:\Data\MySQL\data.sql • Consider the following cases: • Different variations of the presence/absence of the final and the beginning slash in the first and second path:C:\folder + file.txtC:\folder + \file.txtC:\folder\ + file.txtC:\folder\ + \file.txt • If the first path is empty:" + file.txt • If the second path is absolute:C:\folder + D:\folder2\file.txt
Example 2 • First test
Example 2 • Implementation of the function:
Example 2 • Modify the function:
Example 2 • Modify function:
Example 2 • Modify the function:
Example 2 • Finally the function looks like:
Techniques of TDD • Isolated Test • Test List • Assertion First • Test Data • Evident Data
Isolated Test • If you do not pass one test, others should not fall. • If the tests are isolated, the order of their running does not matter. • Tests should not use general resources. General resources used by the tests should not be changed during the test.
Test List • Write down all the tests you want to implement and adhere to this list. • After the green line you must always add one test, so do not just programming all tests at once. • Tests in which there is a need in the process of writing another test, must be simply entered in the test list.
Assertion First • This approach allows you to instantly answer two important questions: “What is the correct result of the test?” and “How I can confirm that it is correct?”. • Firstly we define what do we want to get and then we create the necessary conditions to pass the test (assert). • We should not have too much claims in a test (perfect ─ one, maximum ─ three).
Test Data • Do not use the same data. • If there is no difference between 1 and 2, get 1. • If three test sets will give you the same result as 10 test sets, use three. • Use realistic test data.
Evident Data • It should be obvious where is this or that result from. • Do not hide the calculation of the constants, so it will be clear what needs to be programmed. • Test code should be read from the first time.
Evident Data • Or more evident data:
Tools for unit testing • Java: JUnit; • C++: CppUnit, Boost Test; • Delphi: DUnit; • PHP: PHPUnit; • С#: NUnit. • The full list of tools is here: http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks
Terminology • Test method is a method that checks the behaviourof your object. • Assertion is a comparison of expected and actual results. • Method fixture is a set of operations performed before and after each test method. • Class fixture is a set of operations performed before and after all test methods in the class.
NUnit • NUnit - an open environment unit-test applications for .NET (including the platform Mono). • It was ported from the java language and written in J#. • The new version is written in C# with such innovations as attributes.
NUnit • For the organization of tests the following attributes are used: • [Test] marks the test method. • [TestFixture] marks the class with a set of tests. • [SetUp], [TearDown] mark any procedure without parameters such as fixture level method.
NUnit • The class “Assert” contains static methods of verification of actual values with the expected: • AreEqual, AreNotEqual. • AreSame, AreNotSame. • IsTrue, IsFalse. • Greater, GreaterOrEqual, • IsNotNull, IsNull.