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

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
 

Ä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

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
 
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
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
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
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
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
 
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
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
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
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
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
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
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
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
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
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 

Kürzlich hochgeladen (20)

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
 
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...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
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
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
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
 
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
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
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
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
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
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
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
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
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
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 

Writing good unit tests