Your SlideShare is downloading. ×
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Writing good unit tests
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Writing good unit tests

572

Published on

A brief presentation on some aspects of good unit tests and anti-patterns to watch out to avoid when writing unit tests.

A brief presentation on some aspects of good unit tests and anti-patterns to watch out to avoid when writing unit tests.

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
572
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
30
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Writing good unit tests AgileKZN – November 2012 © Chillisoft 2012 1
  • 2. 2
  • 3. 3
  • 4. Unit test Unit test Unit test Unit test Integration test Unit Unit Unit Unit Unit Unit Unit Unit test test test Unit Unit test test test test Unit test Unit Unit test test Unit Unit test Unit Unit Unit Unit test test Unit test test Unit Unit test test test test Unit test Unit Unit Unit test test Unit Unit Unit test Unit Unit Unit test test test Unit Unit Unit test test test Unit test test test test test test Unit test Unit Unit Unit Unit Unit test Unit test Unit Unit Unit test Unit test test test test test Unit test test Unit Unit test Unit Unit Unit Unit Unit test test test Unit Unit Unit test test Unit test test Unit test test Unit test test Unit Unit test Unit Unit Unit test Unit test Unit Unit test test Unit Unit test Unit test test Unit Unit Unit test test test test test test test test Unit test Unit Unit Unit Unit Unit Unit test Unit test test test test Unit Unit test Unit Unit test Unit Unit test Unit Unit Unit Unit test test test Unit test test test test test test test Unit Unit Unit test Unit Unit Unit Unit test test test Unit Unit Unit Unit Unit Unit Unit test test test test Unit test test test test test Unit test test Unit test Unit Unit Unit test Unit Unit Unit Unit Unit test Unit test test test Unit Unit Unit test test test test test test Unit Unit Unit test test test Unit Unit Unit Unit Unit Unit test test test Unit test test test test test test Unit Unit test Unit Unit Unit Unit test test test test Unit Unit Unit Unit test test Unit Unit Unit test test Unit test Unit test Unit test test test test test test Acceptance test Integration test Unit test Unit test Integration test Acceptance test Integration test Integration test Integration Integration test test Acceptance test Integration test Integration test Integration test Integration test Acceptance test Integration test 4
  • 5. 5
  • 6. 6
  • 7. Unit test Unit test Unit test Unit test Integration test Unit Unit Unit Unit Unit Unit Unit Unit test test test Unit Unit test test test test Unit test Unit Unit test test Unit Unit test Unit Unit Unit Unit test test Unit test test Unit Unit test test test test Unit test Unit Unit Unit test test Unit Unit Unit test Unit Unit Unit test test test Unit Unit Unit test test test Unit test test test test test test Unit test Unit Unit Unit Unit Unit test Unit test Unit Unit Unit test Unit test test test test test Unit test test Unit Unit test Unit Unit Unit Unit Unit test test test Unit Unit Unit test test Unit test test Unit test test Unit test test Unit Unit test Unit Unit Unit test Unit test Unit Unit test test Unit Unit test Unit test test Unit Unit Unit test test test test test test test test Unit test Unit Unit Unit Unit Unit Unit test Unit test test test test Unit Unit test Unit Unit test Unit Unit test Unit Unit Unit Unit test test test Unit test test test test test test test Unit Unit Unit test Unit Unit Unit Unit test test test Unit Unit Unit Unit Unit Unit Unit test test test test Unit test test test test test Unit test test Unit test Unit Unit Unit test Unit Unit Unit Unit Unit test Unit test test test Unit Unit Unit test test test test test test Unit Unit Unit test test test Unit Unit Unit Unit Unit Unit test test test Unit test test test test test test Unit Unit test Unit Unit Unit Unit test test test test Unit Unit Unit Unit test test Unit Unit Unit test test Unit test Unit test Unit test test test test test test Acceptance test Integration test Unit test Unit test Integration test Acceptance test Integration test Integration test Integration Integration test test Acceptance test Integration test Integration test Integration test Integration test Acceptance test Integration test 7
  • 8. Unit test 8
  • 9. Unit test 9
  • 10. Unit test Boom! 10
  • 11. 11
  • 12. 12
  • 13. 13
  • 14. 14
  • 15. Unit test 15
  • 16. 16
  • 17. 17
  • 18. 18
  • 19. 19
  • 20. 20
  • 21. 21
  • 22. 22
  • 23. 23
  • 24. 24
  • 25. [Test] public void TestDelimitedTableNameWithSpaces() { //... } [Test] public void Generate_GivenTableNameWithSpaces_ShouldDelimitTableName() { //... } 25
  • 26. [Test] public void Append_GivenEmptyString_ShouldNotAddToPrintItems() { // Arrange var document = CreatePrintableDocument(); // Act document.Append(""); // Assert Assert.AreEqual(0, document.PrintItems.Count); } 26
  • 27. 27
  • 28. 28
  • 29. [Test] public void TestEncryptedPassword() { Assert.AreEqual(encryptedPassword, encryptedConfig.Password); Assert.AreEqual(encryptedPassword, encryptedConfig.DecryptedPassword); encryptedConfig.SetPrivateKey(rsa.ToXmlString(true)); Assert.AreEqual(password, encryptedConfig.DecryptedPassword); } 29
  • 30. [Test] public void TestDelimitedTableNameWithSpaces() { ClassDef.ClassDefs.Clear(); TestAutoInc.LoadClassDefWithAutoIncrementingID(); TestAutoInc bo = new TestAutoInc(); ClassDef.ClassDefs[typeof (TestAutoInc)].TableName = "test autoinc"; DeleteStatementGenerator gen = new DeleteStatementGenerator(bo, DatabaseConnection.CurrentConnection); var statementCol = gen.Generate(); ISqlStatement statement = statementCol.First(); StringAssert.Contains("`test autoinc`", statement.Statement.ToString()); } 30
  • 31. 31
  • 32. 32
  • 33. 33
  • 34. [Test] public void TestDeleteFlagsSetContactPerson() { ContactPerson myContact = new ContactPerson(); Assert.IsTrue(myContact.Status.IsNew); // this object is new myContact.DateOfBirth = new DateTime(1980, 01, 20); myContact.FirstName = "Bob"; myContact.Surname = "Smith"; myContact.Save(); //save the object to the DB Assert.IsFalse(myContact.Status.IsNew); // this object is saved and thus no longer // new Assert.IsFalse(myContact.Status.IsDeleted); IPrimaryKey id = myContact.ID; //Save the objectsID so that it can be loaded from the Data Assert.AreEqual(id, myContact.ID); myContact.MarkForDelete(); Assert.IsTrue(myContact.Status.IsDeleted); myContact.Save(); Assert.IsTrue(myContact.Status.IsDeleted); Assert.IsTrue(myContact.Status.IsNew); } 34
  • 35. Unit test 35
  • 36. [Test] public void GetCurrentCredentialsString_ShouldReturnDeviceIDFromDeviceState() { //Arrange var deviceState = Substitute.For<IDeviceState>(); var id = RandomValueGen.GetRandomGuid(); deviceState.GetDeviceID().Returns(id); var credentialsProvider = new CredentialsProvider(deviceState); //Act var result = credentialsProvider.GetCurrentCredentialsString(); //Assert Assert.That(result, Is.EqualTo(id.ToString()) ); } 36
  • 37. [TestFixtureSetUp] public void TestFixtureSetup() { SetupDBConnection(); DeleteAllContactPersons(); ClassDef.ClassDefs.Clear(); new Car(); CreateUpdatedContactPersonTestPack(); CreateSaveContactPersonTestPack(); CreateDeletedPersonTestPack(); } [Test] public void TestActivatorCreate() { Activator.CreateInstance(typeof (ContactPerson), true); } 37
  • 38. • • • 38
  • 39. 39
  • 40. 40
  • 41. 41
  • 42. 42

×