SlideShare ist ein Scribd-Unternehmen logo
1 von 42
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
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
6
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
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_ShouldDelimitTableName()
{
//...
}

25
[Test]
public void Append_GivenEmptyString_ShouldNotAddToPrintItems()
{
// Arrange
var document = CreatePrintableDocument();
// Act
document.Append("");
// Assert
Assert.AreEqual(0, document.PrintItems.Count);
}

26
27
28
[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
[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
32
33
[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
Unit
test

35
[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
[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
39
40
41
42

Weitere ähnliche Inhalte

Ähnlich wie Writing good unit tests (6)

Testing in java
Testing in javaTesting in java
Testing in java
 
test
testtest
test
 
test
testtest
test
 
Unit testing and junit
Unit testing and junitUnit testing and junit
Unit testing and junit
 
Fundamentals of Testing 2
Fundamentals of Testing 2Fundamentals of Testing 2
Fundamentals of Testing 2
 
Project Onion unit test environment
Project Onion unit test environmentProject Onion unit test environment
Project Onion unit test environment
 

Kürzlich hochgeladen

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Kürzlich hochgeladen (20)

Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 

Writing good unit tests