Writing good unit tests
AgileKZN – November 2012
© Chillisoft 2012

1
2
3
Unit
test
Unit
test

Unit
test

Unit
test

Integration
test

Unit
Unit
Unit
Unit
Unit
Unit
Unit
Unit test
test
test
Unit
U...
5
6
Unit
test
Unit
test

Unit
test

Unit
test

Integration
test

Unit
Unit
Unit
Unit
Unit
Unit
Unit
Unit test
test
test
Unit
U...
Unit
test

8
Unit
test

9
Unit
test

Boom!

10
11
12
13
14
Unit
test

15
16
17
18
19
20
21
22
23
24
[Test]
public void TestDelimitedTableNameWithSpaces()
{
//...
}

[Test]
public void Generate_GivenTableNameWithSpaces_Shou...
[Test]
public void Append_GivenEmptyString_ShouldNotAddToPrintItems()
{
// Arrange
var document = CreatePrintableDocument(...
27
28
[Test]
public void TestEncryptedPassword()
{
Assert.AreEqual(encryptedPassword, encryptedConfig.Password);
Assert.AreEqual...
[Test]
public void TestDelimitedTableNameWithSpaces()
{
ClassDef.ClassDefs.Clear();
TestAutoInc.LoadClassDefWithAutoIncrem...
31
32
33
[Test]
public void TestDeleteFlagsSetContactPerson()
{
ContactPerson myContact = new ContactPerson();
Assert.IsTrue(myCont...
Unit
test

35
[Test]
public void
GetCurrentCredentialsString_ShouldReturnDeviceIDFromDeviceState()
{
//Arrange
var deviceState = Substit...
[TestFixtureSetUp]
public void TestFixtureSetup()
{
SetupDBConnection();
DeleteAllContactPersons();
ClassDef.ClassDefs.Cle...
•
•
•

38
39
40
41
42
Upcoming SlideShare
Loading in...5
×

Writing good unit tests

651

Published on

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
651
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
32
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Writing good unit tests

  1. 1. Writing good unit tests AgileKZN – November 2012 © Chillisoft 2012 1
  2. 2. 2
  3. 3. 3
  4. 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. 5
  6. 6. 6
  7. 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. 8. Unit test 8
  9. 9. Unit test 9
  10. 10. Unit test Boom! 10
  11. 11. 11
  12. 12. 12
  13. 13. 13
  14. 14. 14
  15. 15. Unit test 15
  16. 16. 16
  17. 17. 17
  18. 18. 18
  19. 19. 19
  20. 20. 20
  21. 21. 21
  22. 22. 22
  23. 23. 23
  24. 24. 24
  25. 25. [Test] public void TestDelimitedTableNameWithSpaces() { //... } [Test] public void Generate_GivenTableNameWithSpaces_ShouldDelimitTableName() { //... } 25
  26. 26. [Test] public void Append_GivenEmptyString_ShouldNotAddToPrintItems() { // Arrange var document = CreatePrintableDocument(); // Act document.Append(""); // Assert Assert.AreEqual(0, document.PrintItems.Count); } 26
  27. 27. 27
  28. 28. 28
  29. 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. 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. 31
  32. 32. 32
  33. 33. 33
  34. 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. 35. Unit test 35
  36. 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. 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. • • • 38
  39. 39. 39
  40. 40. 40
  41. 41. 41
  42. 42. 42
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×