Presentation is loading. Please wait.

Presentation is loading. Please wait.

TEST-DRIVEN DEVELOPMENT Lecture 3. Definition Test-driven development (development through testing) is a technique of programming, in which the unit tests.

Similar presentations

Presentation on theme: "TEST-DRIVEN DEVELOPMENT Lecture 3. Definition Test-driven development (development through testing) is a technique of programming, in which the unit tests."— Presentation transcript:


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

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

4 Development cycle (scheme) 4

5 Development cycle 1. The programming system is extracted from the repository if it is in a consistent state, when the full set of unit tests run successfully. 2. 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. 3. 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. 4. 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. 5

6 Development cycle (continuation) 5. The full set of tests runs successfully. 6. Now, when the required functionality is achieved in the simplest way, the program is refactored to improve the structure and eliminate redundant, duplicated code. 7. The full set of tests runs successfully. 8. 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. 6

7 Example 1. Fibonacci Numbers The Fibonacci sequence is determined by the following relation: F(n) = F(n - 1) + F(n - 2) F(1) = 1 F(0) = 0 You want to write a function to determine the n-th number of the sequence. 7

8 Example 1 The first test: The implementation of function: 8

9 Example 1 9

10 Check the second number of the sequence 10

11 Example 1 11

12 Example 1 Modify the function 12

13 Example 1 13

14 Example 1 Add new test, checking the 3 rd and the 4 th numbers of the sequence: 14

15 Example 1 15

16 Example 1 Modify the function 16

17 Example 1 17

18 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.txt C:\folder + \file.txt C:\folder\ + file.txt C:\folder\ + \file.txt If the first path is empty: " + file.txt If the second path is absolute: C:\folder + D:\folder2\file.txt 18

19 Example 2 First test 19

20 Example 2 Implementation of the function: 20

21 Example 2 21

22 Example 2 Modify the function: 22

23 Example 2 23

24 Example 2 24

25 Example 2 25

26 Example 2 Modify function: 26

27 Example 2 27

28 Example 2 28

29 Example 2 29

30 Example 2 Modify the function: 30

31 Example 2 31

32 Example 2 Finally the function looks like: 32

33 Techniques of TDD Isolated Test Test List Assertion First Test Data Evident Data 33

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

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

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

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

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

39 Evident Data Or more evident data: 39

40 Tools for unit testing Java: JUnit; C++: CppUnit, Boost Test; Delphi: DUnit; PHP: PHPUnit; С#: NUnit. The full list of tools is here: 40

41 Terminology Test method is a method that checks the behaviour of 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. 41

42 The order of calling 42

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

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

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

46 NUnit 46

Download ppt "TEST-DRIVEN DEVELOPMENT Lecture 3. Definition Test-driven development (development through testing) is a technique of programming, in which the unit tests."

Similar presentations

Ads by Google