SlideShare a Scribd company logo
4
Most read
5
Most read
6
Most read
Unit Testing with xUnit.net - Part 2
•
•
•
•
•
Unit Testing with xUnit.net - Part 2
Unit Testing with xUnit.net - Part 2
Facts Theories
Constructor and Dispose Class Fixtures
shared object instance across
tests in a single class
Collection Fixtures
shared object instances across
multiple test classes
Unit Testing with xUnit.net - Part 2
Assert AssertExtensions
Before/After UseCulture
Class Fixtures ClassFixtureExample
Collection Fixtures CollectionFixtureExample
Theory Data ExcelDataExample
Test Ordering TestOrderExamples
Traits TraitExtensibility
FactAttribute TheoryAttribute
Test frameworks
Runners
Unit Testing with xUnit.net - Part 2
Mock objects
 Do behavioral assertions for SUT
 Only cares about whether the unit to be tested is correct
 Do not care about the correctness of DOC
Test Code
SUT
DOC
DON’T
CARE!!
●
● replace the real Y with a fake Y
● Benefits
●
●
●
Only test one thing (X) at a time
Faster tests (Y may be slow)
Simpler (Y may depend on Z etc)
● Examples:
●
●
●
●
Time
Database
Email
HttpContext
Notifier
EmailSvc
IEmailSvc
EmailSvcStub
NotifierTest
When testing an object X, that depends on an object Y
● Hand crafted
● More effort to write
● Easier to maintain
● Can be more "black box" than mocks
● Mocks are automatically generated stubs
● Easy to use
● More "magical"
● More effort to maintain
public class EmailSvcStub : IEmailSvc
{
public int NumberOfEmailsSent { get; set; }
public void SendEmail()
{
++NumberOfEmailsSent;
}
}
[Test]
public void Trigger()
{
// setup
var emailSvc = new
EmailSvcStub(); var notifier
= new Notifier(emailSvc);
// invoke
notifier.T
rigger();
// verify
Assert.That(emailSvc.NumberOfEmailsSent,
Is.EqualTo(1));
}
[Test]
public void Trigger()
{
// setup
var emailSvc = Substitute.For<IEmailSvc>();
var notifier = new Notifier(emailSvc);
// invoke
notifier.Trigger();
// verify
emailSvc.Received(1).SendEmail();
}
Unit Testing with xUnit.net - Part 2
Unit Testing with xUnit.net - Part 2
Unit Testing with xUnit.net - Part 2

More Related Content

PPTX
Unit Testing Concepts and Best Practices
PPSX
Unit Test Presentation
ODP
Beginners - Get Started With Unit Testing in .NET
PPTX
Unit Testing
PPTX
Understanding Unit Testing
PPTX
Unit Testing And Mocking
PDF
JUnit & Mockito, first steps
Unit Testing Concepts and Best Practices
Unit Test Presentation
Beginners - Get Started With Unit Testing in .NET
Unit Testing
Understanding Unit Testing
Unit Testing And Mocking
JUnit & Mockito, first steps

What's hot (20)

PPTX
Unit Testing in Swift
PDF
Test Driven Development (TDD)
PPTX
Moq Presentation
PDF
Unit testing best practices
PPT
Test Driven Development
PPT
PDF
Workshop unit test
PDF
Clean Unit Test Patterns
PPTX
Unit Testing in .NET Core 7.0 with XUnit.pptx
PPTX
Unit test
PPTX
TDD - Agile
PDF
C++ Unit Test with Google Testing Framework
PPTX
Unit tests & TDD
PPTX
Saving Time By Testing With Jest
PPTX
Automation testing & Unit testing
PDF
Unit Testing with Jest
PDF
Kill the mutants - A better way to test your tests
PPTX
An Introduction to Unit Testing
PPTX
Mocking with Mockito
Unit Testing in Swift
Test Driven Development (TDD)
Moq Presentation
Unit testing best practices
Test Driven Development
Workshop unit test
Clean Unit Test Patterns
Unit Testing in .NET Core 7.0 with XUnit.pptx
Unit test
TDD - Agile
C++ Unit Test with Google Testing Framework
Unit tests & TDD
Saving Time By Testing With Jest
Automation testing & Unit testing
Unit Testing with Jest
Kill the mutants - A better way to test your tests
An Introduction to Unit Testing
Mocking with Mockito
Ad

Similar to Unit Testing with xUnit.net - Part 2 (20)

PPTX
Test-Driven Development
PPTX
RIA 06 & 07 - Unit Testing in Detail
PDF
TDD Workshop UTN 2012
PPTX
Modern ASP.NET Webskills
PPTX
Type mock isolator
PPTX
Unit Testing Full@
PDF
Test and Behaviour Driven Development (TDD/BDD)
PDF
Unit Testing and role of Test doubles
PPTX
Implementing TDD in for .net Core applications
PPT
Xp Day 080506 Unit Tests And Mocks
PPT
Testing Software Engineering systems end to end
PPTX
Tdd & unit test
PPTX
Moq presentation
PPTX
Mock driven development using .NET
PPTX
Unit tests and TDD
PPTX
Rc2010 tdd
PPTX
Skillwise Unit Testing
PDF
Introduction to Unit Tests and TDD
PPTX
Refactoring Away from Test Hell
Test-Driven Development
RIA 06 & 07 - Unit Testing in Detail
TDD Workshop UTN 2012
Modern ASP.NET Webskills
Type mock isolator
Unit Testing Full@
Test and Behaviour Driven Development (TDD/BDD)
Unit Testing and role of Test doubles
Implementing TDD in for .net Core applications
Xp Day 080506 Unit Tests And Mocks
Testing Software Engineering systems end to end
Tdd & unit test
Moq presentation
Mock driven development using .NET
Unit tests and TDD
Rc2010 tdd
Skillwise Unit Testing
Introduction to Unit Tests and TDD
Refactoring Away from Test Hell
Ad

More from BizTalk360 (20)

PPTX
Optimise Business Activity Tracking – Insights from Smurfit Kappa
PPTX
Optimise Business Activity Tracking – Insights from Smurfit Kappa
PPTX
What's inside "migrating to biz talk server 2020" Book (BizTalk360 Webinar)
PPTX
Integration Monday - Logic Apps: Development Experiences
PPTX
Integration Monday - BizTalk Migrator Deep Dive
PPTX
Testing for Logic App Solutions | Integration Monday
PPTX
No-Slides
PPTX
System Integration using Reactive Programming | Integration Monday
PPTX
Building workflow solution with Microsoft Azure and Cloud | Integration Monday
PPTX
Serverless Minimalism: How to architect your apps to save 98% on your Azure b...
PPTX
Migrating BizTalk Solutions to Azure: Mapping Messages | Integration Monday
PPTX
Integration-Monday-Infrastructure-As-Code-With-Terraform
PDF
Integration-Monday-Stateful-Programming-Models-Serverless-Functions
PPTX
Integration-Monday-Serverless-Slackbots-with-Azure-Durable-Functions
PPTX
Integration-Monday-Building-Stateful-Workloads-Kubernetes
PPTX
Integration-Monday-Logic-Apps-Tips-Tricks
PPTX
Integration-Monday-Terraform-Serverless
PPTX
Integration-Monday-Microsoft-Power-Platform
PDF
One name unify them all
PPTX
Securely Publishing Azure Services
Optimise Business Activity Tracking – Insights from Smurfit Kappa
Optimise Business Activity Tracking – Insights from Smurfit Kappa
What's inside "migrating to biz talk server 2020" Book (BizTalk360 Webinar)
Integration Monday - Logic Apps: Development Experiences
Integration Monday - BizTalk Migrator Deep Dive
Testing for Logic App Solutions | Integration Monday
No-Slides
System Integration using Reactive Programming | Integration Monday
Building workflow solution with Microsoft Azure and Cloud | Integration Monday
Serverless Minimalism: How to architect your apps to save 98% on your Azure b...
Migrating BizTalk Solutions to Azure: Mapping Messages | Integration Monday
Integration-Monday-Infrastructure-As-Code-With-Terraform
Integration-Monday-Stateful-Programming-Models-Serverless-Functions
Integration-Monday-Serverless-Slackbots-with-Azure-Durable-Functions
Integration-Monday-Building-Stateful-Workloads-Kubernetes
Integration-Monday-Logic-Apps-Tips-Tricks
Integration-Monday-Terraform-Serverless
Integration-Monday-Microsoft-Power-Platform
One name unify them all
Securely Publishing Azure Services

Recently uploaded (20)

PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
cuic standard and advanced reporting.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Encapsulation theory and applications.pdf
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPT
Teaching material agriculture food technology
PDF
Electronic commerce courselecture one. Pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Diabetes mellitus diagnosis method based random forest with bat algorithm
cuic standard and advanced reporting.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Encapsulation theory and applications.pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
20250228 LYD VKU AI Blended-Learning.pptx
Network Security Unit 5.pdf for BCA BBA.
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Spectral efficient network and resource selection model in 5G networks
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Review of recent advances in non-invasive hemoglobin estimation
Teaching material agriculture food technology
Electronic commerce courselecture one. Pdf

Unit Testing with xUnit.net - Part 2

  • 6. Constructor and Dispose Class Fixtures shared object instance across tests in a single class Collection Fixtures shared object instances across multiple test classes
  • 8. Assert AssertExtensions Before/After UseCulture Class Fixtures ClassFixtureExample Collection Fixtures CollectionFixtureExample Theory Data ExcelDataExample Test Ordering TestOrderExamples Traits TraitExtensibility FactAttribute TheoryAttribute Test frameworks Runners
  • 10. Mock objects  Do behavioral assertions for SUT  Only cares about whether the unit to be tested is correct  Do not care about the correctness of DOC Test Code SUT DOC DON’T CARE!!
  • 11. ● ● replace the real Y with a fake Y ● Benefits ● ● ● Only test one thing (X) at a time Faster tests (Y may be slow) Simpler (Y may depend on Z etc) ● Examples: ● ● ● ● Time Database Email HttpContext Notifier EmailSvc IEmailSvc EmailSvcStub NotifierTest When testing an object X, that depends on an object Y
  • 12. ● Hand crafted ● More effort to write ● Easier to maintain ● Can be more "black box" than mocks
  • 13. ● Mocks are automatically generated stubs ● Easy to use ● More "magical" ● More effort to maintain
  • 14. public class EmailSvcStub : IEmailSvc { public int NumberOfEmailsSent { get; set; } public void SendEmail() { ++NumberOfEmailsSent; } } [Test] public void Trigger() { // setup var emailSvc = new EmailSvcStub(); var notifier = new Notifier(emailSvc); // invoke notifier.T rigger(); // verify Assert.That(emailSvc.NumberOfEmailsSent, Is.EqualTo(1)); }
  • 15. [Test] public void Trigger() { // setup var emailSvc = Substitute.For<IEmailSvc>(); var notifier = new Notifier(emailSvc); // invoke notifier.Trigger(); // verify emailSvc.Received(1).SendEmail(); }