a method) that invokes another piece of code and checks the correctness of some assumptions afterward. If the assumptions turn out to be wrong, the unit test has failed. 2
a small piece of code you want to test (also known as the system under test, or SUT) • Act: making some action with system under test (usually it’s a method calling) • Assert: checking the results 3
[TestMethod] public void ParseTest() { // Arrange var parser = new SimpleParser(); // Act var result = parser.Parse("123"); // Assert Assert.AreEqual(result, 123); } } 4
have enough time for writing tests.” • ”Testing in monotonous and boring.” • ”I’m not a tester!” • ”I don’t know how to test it.” • ”But it works! It doesn’t need tests.” 7
testing are not very interesting and explain only theory • But errors prediction and prevention are very responsible activities • Not every developer is able to testing own (and someone else’s) code • ! It’s a valuable skill • Endless debug is more boring than writing tests 9
have you written this code? • And how will you be using it? • Maybe there are some problems in project design (testable code often is better designed) 11
perfect developer: • He have read all needed books • He knows all design patterns • He perfectly knows every line of code of his 515 classes • How do you think, after changing one line of code in one class can he accurately predict impact on the other 514 classes? • And what about not so perfect developers? • Maybe it’s more easier to use unit tests and don’t keep in mind extra details? 12
object created to stand in for an object that your code will be collaborating with. Your code can call methods on the mock object, which will deliver results as set up by your tests.” Source: ”JUnit in Action”, Vincent Massol 15
and repeatable • It should be easy to implement • Once it’s written, it should remain for future use • Anyone should be able to run it • It should run at the push of a button 17
and repeatable • It should be easy to implement • Once it’s written, it should remain for future use • Anyone should be able to run it • It should run at the push of a button • It should run quickly 17
per class under test • Class: LoginManager • Test class: LoginManagerTests • One test class per feature • Class: LoginManager • Test classes: LoginManagerTests, LoginManagerTestsChangePassword 19
method per method under test • Method: ChangePassword() • Test method: ChangePasswordTest() • One test method per method under test with concrete input values and expected behavior • Method: ChangePassword() • Test methods: ChangePassword_EmptyPassword_ReturnsError(), ChangePassword_InvalidPassword_ReturnsValidationError() 20