SlideShare una empresa de Scribd logo
1 de 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

Más contenido relacionado

Similar a Writing good unit tests

Unit testing and junit
Unit testing and junitUnit testing and junit
Unit testing and junitÖmer Taşkın
 
Fundamentals of Testing 2
Fundamentals of Testing 2Fundamentals of Testing 2
Fundamentals of Testing 2Hoang Nguyen
 
Project Onion unit test environment
Project Onion unit test environmentProject Onion unit test environment
Project Onion unit test environmentAbhinav Jha
 

Similar a 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
 

Último

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 WorkerThousandEyes
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
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 Processorsdebabhi2
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
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...Martijn de Jong
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
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 WorkerThousandEyes
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
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 2024The Digital Insurer
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 

Último (20)

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
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
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
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
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...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
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
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
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
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 

Writing good unit tests