SlideShare uma empresa Scribd logo
1 de 31
Using xUnit as a Swiss-Army Testing Toolkit
(Does ‘Unit’ Size Matter?)
ACCU Conference 2011
Chris Oldwood
gort@cix.co.uk
Stream of Consciousness
• Developer Driven Testing
• The Essence of (x)Unit Testing
• Those Pesky Dependencies
• Code & Test Evolution in Practice
Stream of Consciousness
• Developer Driven Testing
• The Essence of (x)Unit Testing
• Those Pesky Dependencies
• Code & Test Evolution in Practice
Text Book Test
string[][] tests =
{
{ "3", "4", "+", "7" },
{ "9", "1", "-", "8" },
{ "2", "3", "*", "6" },
{ "9", "3", "/", "3" },
};
void run_tests()
{
var calculator = new Calculator();
foreach(var test in tests)
{
var lhs = test[0];
var rhs = test[1];
var op = test[2];
var result = calculator(lhs, rhs, op);
assert(result == test[3]);
}
}
Exercise Left for the Reader
External System 1 External System 2 External System 3
The System
42
ServicesDatabase
Unit
Integration
System Component
Stress
Lexicon of Testing
End-to-End
Regression
White Box
Black Box
Characterisation
Exploration
System
Integration
Component
Unit
Dependencies
Feedback
‘Unit’ Evolution
All Regression
Stream of Consciousness
• Developer Driven Testing
• The Essence of (x)Unit Testing
• Those Pesky Dependencies
• Code & Test Evolution in Practice
Test == Specification
public void Execute_Should_Elide_Agreement_When_No_Trades_Match()
{
var trades = new List<Trade> { new Trade("trade-id", "product-a") };
var agreement = new Agreement("product-b");
var task = new PreparationTask(trades, agreement);
var result = task.Execute(s_services);
Assert.That(task.Agreement, Is.Null);
}
Consistent Style
public void a_c_sharp_test()
{
var arrangement = new Arrangement();
var result = arrangement.action();
Assert.That(result, Is.EqualTo(expectation));
}
create procedure a_sql_test
as
declare arrangement varchar(100),
result varchar(100)
exec action @input = arrangement,
@output = result
exec AssertAreEqual @result, "expectation"
go
Minimises Dependencies
MyService
External Service DatabaseFile System
Mock External
Service
IExternalService IFileSystem IDatabase
Mock File
System
Mock Database
Promotes Arbitrary Code Execution
public void Prepare_Should_Elide_Agreement_When_No_Trades_Match()
{
var trades = new List<Trade> { new Trade("trade-id", "product-a") };
var agreement = new Agreement("product-b");
var task = new PreparationTask(trades, agreement);
var result = task.Execute(s_services);
Assert.That(task.Agreement, Is.Null);
}
LibraryEXE Stub
Test Runner LibraryTestsDebugger
Custom Test
Harness
Automated Testing
• Lowers the barrier to running tests
• Regression testing is implicit
• Build server watches your back
Stream of Consciousness
• Developer Driven Testing
• The Essence of (x)Unit Testing
• Those Pesky Dependencies
• Code & Test Evolution in Practice
Pesky Dependencies
External System 1 External System 2 External System 3
The System
Service 2
Service 1
File-System
Database
xUnit Abuse
• Fight the Shadow Cache
• Invoke TearDown from SetUp
• Test/build failure isn’t absolute
File-System (Reading)
• Source Control directory
• Build server directory
• Resource files
File-System (Writing)
• TEMP directory
• Output directory
Database
• Per-user / per-branch workspace
• Only need schema not data (Integration)
• Can reuse existing unit test database
• Use same code revision for compatibility
• Use transactions to avoid residual effects
• Fake tables with CSV files
Database Asserts
public void AddCustomer_Should_Persist_The_Customer()
{
const id = 1234;
const name = "name";
var customer = new Customer(. . .);
using (var connection = AcquireConnection())
{
CustomerDataMapper.AddCustomer(customer, connection);
Assert.That(RowExists("dbo.Customer",
" CustomerId = {0}"
+ " AND CustomerName = '{1}'",
id, name),
Is.True);
}
}
Database SetUp/TearDown
[TestFixture, TestCategory.DatabaseTest]
public class SomeEntityTests : DatabaseTestBase
{
[TestFixtureSetUp]
public void FixtureSetUp
{
using(var connection = AcquireConnection())
{
connection.Execute("insert into thingy_table values(1, 2, 3)");
connection.Execute("test.InsertThingy(1, 2, 3)");
}
}
[TestFixtureTearDown]
public void FixtureTearDown
{
using(var connection = AcquireConnection())
{
connection.Execute("delete from thingy_table");
connection.Execute("test.DeleteAllThingys");
}
}
}
Helper Base Class
public class DatabaseTestBase
{
public ISqlConnection AcquireConnection()
{
return . . .
}
. . .
public bool RowExists(string table, string where, string params[])
{
string filter = String.Format(where, params);
string sql = String.Format(
"select count(*) as [Count] from {0} where {1}"
, table, filter);
using (var connection = AcquireConnection())
{
var reader = connection.ExecuteQuery(sql);
return (reader.GetInt("Count") == 1);
}
}
. . .
}
External Systems
• Verify API behaviour
• Test internal façade
• Reliability varies (DEV vs PROD)
Stream of Consciousness
• Developer Driven Testing
• The Essence of (x)Unit Testing
• Those Pesky Dependencies
• Code & Test Evolution in Practice
System Architecture
Market Data Trade Data Analytics
The System
42
ServicesDatabase
Initial System Test
Market
Data Service
Trade
Data Service
Analytics Service
Calculator
Test Runner
System Tests
[Test, TestCategory.SystemTest]
public void Calculate_Answer()
{
. . .
var result = c.calculate();
Assert.Equal(result, 42);
}
Addressing External Risks
External Market
Data Service API
External Trade
Data Service API
External Market
Data Service Tests
External Trade
Data Service Tests
Test Runner
Internal Service Design
External Service
API
External Service
Tests
Internal Service
External
Service Facade
Internal Service
Tests
Mock
External Services
Performance
Test Runner
Mock Service
Data Access Layer
Database
Public Interface
Database Unit
Tests
Data Access
Layer
Data Access
Layer Tests
Database API
Mock Database
API
Mock Data
Access Layer
Database
Public Interface
External Analytics
Service
External Market
Data Service API
External Market
Data Service API
System Evolution
Mock Market
Data Service
Mock Trade
Data Service
Mock Analytics
Service
Calculator
Test Runner
Unit / Integration
/ System Tests
[Test, TestCategory.SystemTest]
public void Calc_Answer_For_ABC_Plc()
{
. . .
var result = c.calculate();
Assert.Equal(result, 41.75);
}
Mock Data
Access Layer
Market
Data Service
Trade
Data Service
Analytics Service
Data Access
Layer
“The Oldwood Thing”
http://chrisoldwood.blogspot.com
Chris Oldwood
gort@cix.co.uk

Mais conteúdo relacionado

Mais procurados

Indexing and Query Optimizer (Aaron Staple)
Indexing and Query Optimizer (Aaron Staple)Indexing and Query Optimizer (Aaron Staple)
Indexing and Query Optimizer (Aaron Staple)
MongoSF
 
Parameterization is nothing but giving multiple input
Parameterization is nothing but giving multiple inputParameterization is nothing but giving multiple input
Parameterization is nothing but giving multiple input
uanna
 
Creating, Updating and Deleting Document in MongoDB
Creating, Updating and Deleting Document in MongoDBCreating, Updating and Deleting Document in MongoDB
Creating, Updating and Deleting Document in MongoDB
Wildan Maulana
 

Mais procurados (20)

Error based blind sqli
Error based blind sqliError based blind sqli
Error based blind sqli
 
Fighting security trolls_with_high-quality_mindsets
Fighting security trolls_with_high-quality_mindsetsFighting security trolls_with_high-quality_mindsets
Fighting security trolls_with_high-quality_mindsets
 
Hidden Treasures of the Python Standard Library
Hidden Treasures of the Python Standard LibraryHidden Treasures of the Python Standard Library
Hidden Treasures of the Python Standard Library
 
Django and working with large database tables
Django and working with large database tablesDjango and working with large database tables
Django and working with large database tables
 
Python and cassandra
Python and cassandraPython and cassandra
Python and cassandra
 
Indexing and Query Optimizer (Aaron Staple)
Indexing and Query Optimizer (Aaron Staple)Indexing and Query Optimizer (Aaron Staple)
Indexing and Query Optimizer (Aaron Staple)
 
Parameterization is nothing but giving multiple input
Parameterization is nothing but giving multiple inputParameterization is nothing but giving multiple input
Parameterization is nothing but giving multiple input
 
Java OOP Programming language (Part 4) - Collection
Java OOP Programming language (Part 4) - CollectionJava OOP Programming language (Part 4) - Collection
Java OOP Programming language (Part 4) - Collection
 
How to Create Database component -Enterprise Application Using C# Lab
How to Create Database component -Enterprise Application Using C# Lab  How to Create Database component -Enterprise Application Using C# Lab
How to Create Database component -Enterprise Application Using C# Lab
 
Apache Cassandra & Data Modeling
Apache Cassandra & Data ModelingApache Cassandra & Data Modeling
Apache Cassandra & Data Modeling
 
Mongo indexes
Mongo indexesMongo indexes
Mongo indexes
 
Giving Clarity to LINQ Queries by Extending Expressions R2
Giving Clarity to LINQ Queries by Extending Expressions R2Giving Clarity to LINQ Queries by Extending Expressions R2
Giving Clarity to LINQ Queries by Extending Expressions R2
 
MongoDB and Indexes - MUG Denver - 20160329
MongoDB and Indexes - MUG Denver - 20160329MongoDB and Indexes - MUG Denver - 20160329
MongoDB and Indexes - MUG Denver - 20160329
 
Android Architecture components
Android Architecture componentsAndroid Architecture components
Android Architecture components
 
Jdbc oracle
Jdbc oracleJdbc oracle
Jdbc oracle
 
Presentation Android Architecture Components
Presentation Android Architecture ComponentsPresentation Android Architecture Components
Presentation Android Architecture Components
 
4 gouping object
4 gouping object4 gouping object
4 gouping object
 
Creating, Updating and Deleting Document in MongoDB
Creating, Updating and Deleting Document in MongoDBCreating, Updating and Deleting Document in MongoDB
Creating, Updating and Deleting Document in MongoDB
 
Java OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 8) - Java Database JDBCJava OOP Programming language (Part 8) - Java Database JDBC
Java OOP Programming language (Part 8) - Java Database JDBC
 
Data Love Conference - Window Functions for Database Analytics
Data Love Conference - Window Functions for Database AnalyticsData Love Conference - Window Functions for Database Analytics
Data Love Conference - Window Functions for Database Analytics
 

Destaque

Destaque (6)

Introduction to Unit Testing
Introduction to Unit TestingIntroduction to Unit Testing
Introduction to Unit Testing
 
Le Tour de xUnit
Le Tour de xUnitLe Tour de xUnit
Le Tour de xUnit
 
ASP.NET Core 2.0 - .NET São Paulo - Outubro-2017
ASP.NET Core 2.0 - .NET São Paulo - Outubro-2017ASP.NET Core 2.0 - .NET São Paulo - Outubro-2017
ASP.NET Core 2.0 - .NET São Paulo - Outubro-2017
 
xUnit Test Patterns - Chapter19
xUnit Test Patterns - Chapter19xUnit Test Patterns - Chapter19
xUnit Test Patterns - Chapter19
 
Mini training - Moving to xUnit.net
Mini training - Moving to xUnit.netMini training - Moving to xUnit.net
Mini training - Moving to xUnit.net
 
AI and Machine Learning Demystified by Carol Smith at Midwest UX 2017
AI and Machine Learning Demystified by Carol Smith at Midwest UX 2017AI and Machine Learning Demystified by Carol Smith at Midwest UX 2017
AI and Machine Learning Demystified by Carol Smith at Midwest UX 2017
 

Semelhante a Using xUnit as a Swiss-Aarmy Testing Toolkit

33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests
Tomek Kaczanowski
 

Semelhante a Using xUnit as a Swiss-Aarmy Testing Toolkit (20)

Advances in Unit Testing: Theory and Practice
Advances in Unit Testing: Theory and PracticeAdvances in Unit Testing: Theory and Practice
Advances in Unit Testing: Theory and Practice
 
Solr @ Etsy - Apache Lucene Eurocon
Solr @ Etsy - Apache Lucene EuroconSolr @ Etsy - Apache Lucene Eurocon
Solr @ Etsy - Apache Lucene Eurocon
 
Testing basics for developers
Testing basics for developersTesting basics for developers
Testing basics for developers
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests
 
Java Language fundamental
Java Language fundamentalJava Language fundamental
Java Language fundamental
 
Stress test data pipeline
Stress test data pipelineStress test data pipeline
Stress test data pipeline
 
xUnit Style Database Testing
xUnit Style Database TestingxUnit Style Database Testing
xUnit Style Database Testing
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
What is new in Java 8
What is new in Java 8What is new in Java 8
What is new in Java 8
 
Good Practices On Test Automation
Good Practices On Test AutomationGood Practices On Test Automation
Good Practices On Test Automation
 
Static analysis: Around Java in 60 minutes
Static analysis: Around Java in 60 minutesStatic analysis: Around Java in 60 minutes
Static analysis: Around Java in 60 minutes
 
Terraform introduction
Terraform introductionTerraform introduction
Terraform introduction
 
Linq
LinqLinq
Linq
 
Appium TestNG Framework and Multi-Device Automation Execution
Appium TestNG Framework and Multi-Device Automation ExecutionAppium TestNG Framework and Multi-Device Automation Execution
Appium TestNG Framework and Multi-Device Automation Execution
 
Cassandra Day NY 2014: Getting Started with the DataStax C# Driver
Cassandra Day NY 2014: Getting Started with the DataStax C# DriverCassandra Day NY 2014: Getting Started with the DataStax C# Driver
Cassandra Day NY 2014: Getting Started with the DataStax C# Driver
 
Typed? Dynamic? Both! Cross-platform DSLs in C#
Typed? Dynamic? Both! Cross-platform DSLs in C#Typed? Dynamic? Both! Cross-platform DSLs in C#
Typed? Dynamic? Both! Cross-platform DSLs in C#
 
Selenium Webdriver with data driven framework
Selenium Webdriver with data driven frameworkSelenium Webdriver with data driven framework
Selenium Webdriver with data driven framework
 
MongoDB World 2019: Life In Stitch-es
MongoDB World 2019: Life In Stitch-esMongoDB World 2019: Life In Stitch-es
MongoDB World 2019: Life In Stitch-es
 
Unit testing with JUnit
Unit testing with JUnitUnit testing with JUnit
Unit testing with JUnit
 

Mais de Chris Oldwood

Mais de Chris Oldwood (15)

The __far* Side
The __far* SideThe __far* Side
The __far* Side
 
Monolithic Delivery
Monolithic DeliveryMonolithic Delivery
Monolithic Delivery
 
A Test of Strength
A Test of StrengthA Test of Strength
A Test of Strength
 
In The Toolbox - LIVE!
In The Toolbox - LIVE!In The Toolbox - LIVE!
In The Toolbox - LIVE!
 
Test-Driven SQL
Test-Driven SQLTest-Driven SQL
Test-Driven SQL
 
Waltzing with Branches [ACCU]
Waltzing with Branches [ACCU]Waltzing with Branches [ACCU]
Waltzing with Branches [ACCU]
 
Continuous Delivery
Continuous DeliveryContinuous Delivery
Continuous Delivery
 
Becoming a Bitter Programmer
Becoming a Bitter ProgrammerBecoming a Bitter Programmer
Becoming a Bitter Programmer
 
Waltzing with Branches [Agile o/t Beach]
Waltzing with Branches [Agile o/t Beach]Waltzing with Branches [Agile o/t Beach]
Waltzing with Branches [Agile o/t Beach]
 
Robust Software
Robust SoftwareRobust Software
Robust Software
 
Version Control - Patterns and Practices
Version Control - Patterns and PracticesVersion Control - Patterns and Practices
Version Control - Patterns and Practices
 
Requiem (For Windows XP)
Requiem (For Windows XP)Requiem (For Windows XP)
Requiem (For Windows XP)
 
(Re)Reading the Classics
(Re)Reading the Classics(Re)Reading the Classics
(Re)Reading the Classics
 
Recycle Bin 101
Recycle Bin 101Recycle Bin 101
Recycle Bin 101
 
The Art of Code
The Art of CodeThe Art of Code
The Art of Code
 

Último

CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
anilsa9823
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
anilsa9823
 

Último (20)

Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 

Using xUnit as a Swiss-Aarmy Testing Toolkit

  • 1. Using xUnit as a Swiss-Army Testing Toolkit (Does ‘Unit’ Size Matter?) ACCU Conference 2011 Chris Oldwood gort@cix.co.uk
  • 2. Stream of Consciousness • Developer Driven Testing • The Essence of (x)Unit Testing • Those Pesky Dependencies • Code & Test Evolution in Practice
  • 3. Stream of Consciousness • Developer Driven Testing • The Essence of (x)Unit Testing • Those Pesky Dependencies • Code & Test Evolution in Practice
  • 4. Text Book Test string[][] tests = { { "3", "4", "+", "7" }, { "9", "1", "-", "8" }, { "2", "3", "*", "6" }, { "9", "3", "/", "3" }, }; void run_tests() { var calculator = new Calculator(); foreach(var test in tests) { var lhs = test[0]; var rhs = test[1]; var op = test[2]; var result = calculator(lhs, rhs, op); assert(result == test[3]); } }
  • 5. Exercise Left for the Reader External System 1 External System 2 External System 3 The System 42 ServicesDatabase
  • 6. Unit Integration System Component Stress Lexicon of Testing End-to-End Regression White Box Black Box Characterisation Exploration
  • 8. Stream of Consciousness • Developer Driven Testing • The Essence of (x)Unit Testing • Those Pesky Dependencies • Code & Test Evolution in Practice
  • 9. Test == Specification public void Execute_Should_Elide_Agreement_When_No_Trades_Match() { var trades = new List<Trade> { new Trade("trade-id", "product-a") }; var agreement = new Agreement("product-b"); var task = new PreparationTask(trades, agreement); var result = task.Execute(s_services); Assert.That(task.Agreement, Is.Null); }
  • 10. Consistent Style public void a_c_sharp_test() { var arrangement = new Arrangement(); var result = arrangement.action(); Assert.That(result, Is.EqualTo(expectation)); } create procedure a_sql_test as declare arrangement varchar(100), result varchar(100) exec action @input = arrangement, @output = result exec AssertAreEqual @result, "expectation" go
  • 11. Minimises Dependencies MyService External Service DatabaseFile System Mock External Service IExternalService IFileSystem IDatabase Mock File System Mock Database
  • 12. Promotes Arbitrary Code Execution public void Prepare_Should_Elide_Agreement_When_No_Trades_Match() { var trades = new List<Trade> { new Trade("trade-id", "product-a") }; var agreement = new Agreement("product-b"); var task = new PreparationTask(trades, agreement); var result = task.Execute(s_services); Assert.That(task.Agreement, Is.Null); } LibraryEXE Stub Test Runner LibraryTestsDebugger Custom Test Harness
  • 13. Automated Testing • Lowers the barrier to running tests • Regression testing is implicit • Build server watches your back
  • 14. Stream of Consciousness • Developer Driven Testing • The Essence of (x)Unit Testing • Those Pesky Dependencies • Code & Test Evolution in Practice
  • 15. Pesky Dependencies External System 1 External System 2 External System 3 The System Service 2 Service 1 File-System Database
  • 16. xUnit Abuse • Fight the Shadow Cache • Invoke TearDown from SetUp • Test/build failure isn’t absolute
  • 17. File-System (Reading) • Source Control directory • Build server directory • Resource files
  • 18. File-System (Writing) • TEMP directory • Output directory
  • 19. Database • Per-user / per-branch workspace • Only need schema not data (Integration) • Can reuse existing unit test database • Use same code revision for compatibility • Use transactions to avoid residual effects • Fake tables with CSV files
  • 20. Database Asserts public void AddCustomer_Should_Persist_The_Customer() { const id = 1234; const name = "name"; var customer = new Customer(. . .); using (var connection = AcquireConnection()) { CustomerDataMapper.AddCustomer(customer, connection); Assert.That(RowExists("dbo.Customer", " CustomerId = {0}" + " AND CustomerName = '{1}'", id, name), Is.True); } }
  • 21. Database SetUp/TearDown [TestFixture, TestCategory.DatabaseTest] public class SomeEntityTests : DatabaseTestBase { [TestFixtureSetUp] public void FixtureSetUp { using(var connection = AcquireConnection()) { connection.Execute("insert into thingy_table values(1, 2, 3)"); connection.Execute("test.InsertThingy(1, 2, 3)"); } } [TestFixtureTearDown] public void FixtureTearDown { using(var connection = AcquireConnection()) { connection.Execute("delete from thingy_table"); connection.Execute("test.DeleteAllThingys"); } } }
  • 22. Helper Base Class public class DatabaseTestBase { public ISqlConnection AcquireConnection() { return . . . } . . . public bool RowExists(string table, string where, string params[]) { string filter = String.Format(where, params); string sql = String.Format( "select count(*) as [Count] from {0} where {1}" , table, filter); using (var connection = AcquireConnection()) { var reader = connection.ExecuteQuery(sql); return (reader.GetInt("Count") == 1); } } . . . }
  • 23. External Systems • Verify API behaviour • Test internal façade • Reliability varies (DEV vs PROD)
  • 24. Stream of Consciousness • Developer Driven Testing • The Essence of (x)Unit Testing • Those Pesky Dependencies • Code & Test Evolution in Practice
  • 25. System Architecture Market Data Trade Data Analytics The System 42 ServicesDatabase
  • 26. Initial System Test Market Data Service Trade Data Service Analytics Service Calculator Test Runner System Tests [Test, TestCategory.SystemTest] public void Calculate_Answer() { . . . var result = c.calculate(); Assert.Equal(result, 42); }
  • 27. Addressing External Risks External Market Data Service API External Trade Data Service API External Market Data Service Tests External Trade Data Service Tests Test Runner
  • 28. Internal Service Design External Service API External Service Tests Internal Service External Service Facade Internal Service Tests Mock External Services Performance Test Runner Mock Service
  • 29. Data Access Layer Database Public Interface Database Unit Tests Data Access Layer Data Access Layer Tests Database API Mock Database API Mock Data Access Layer
  • 30. Database Public Interface External Analytics Service External Market Data Service API External Market Data Service API System Evolution Mock Market Data Service Mock Trade Data Service Mock Analytics Service Calculator Test Runner Unit / Integration / System Tests [Test, TestCategory.SystemTest] public void Calc_Answer_For_ABC_Plc() { . . . var result = c.calculate(); Assert.Equal(result, 41.75); } Mock Data Access Layer Market Data Service Trade Data Service Analytics Service Data Access Layer

Notas do Editor

  1. -It’s all about testing units as they get bigger and bigger
  2. -Questions at the end
  3. -Simple black box testing with test cases is unrealistic (for me) -Simple scalar inputs - No I/O or threads -What about exceptions?
  4. -Users at periphery– so User Acceptance Tests only a small part -The vast majority of tests are internal and of a technical nature -Crosses subsystems and internal/external systems – lots of I/O, threads etc.
  5. -Different interpretations of what a Unit is -This talk is developer focused, i.e. biased towards white box testing
  6. -Layers of testing just like layers of code -More layers means more dependencies &amp; slower feedback -New tests become regression tests if kept
  7. -Naming is most important aspect and gets harder as you acquire layers -Example test for a workaround (i.e. not in specification) -Most tests are non-trivial and require non-trivial inputs
  8. -xUnit brings familiarity across the technology stack -One size doesn’t fit all but it can fit many cases
  9. -Our old friend Mr Coupling -Programming to an interface (quite literally in modern languages, but not in C/C++)
  10. -All testable logic in libraries, executables just bootstrapping stubs -Faster debug time as it is often easier to reproduce the problem as it requires less setup -Debug service as in-proc process -Script + command line vs Test configuration (cf Resharper)
  11. -Inside vs outside your control (dealing with failed builds)
  12. -Memory is transient – automatic cleanup -Shadow cache can help and hinder -Need to use SetUp &amp; TearDown to ensure no persistent effects after failure -Sometimes tests are inconclusive (external service down)
  13. -FS often considered a dependency acceptable in unit testing -Should you mock the file-system? Yes. -VCS folder is part of source code (ie isolated), relative paths tend to be stable -Build server is fixed common point but needs manual versioning -Resource files (.zip within .rc, unpack to TEMP etc)
  14. -ASSERTS? Test for presence (simple) or compare contents (harder) -TEMP known directory, but for multiple builds need to isolate branches (e.g. use PID) -Output needs configuring but is isolated &amp; also transient
  15. -Desktop vs RDBMS (Lite editions) -Not testing procedures (integration) - testing interaction with procedures -Assume underlying DB is already unit tested -Build on top of unit test database -Use transactions to help avoid residual effects - control the connection pool and therefore the transaction lifetime
  16. -For entities - row exists, n rows exist -Do you check all attributes or just entity ID? -Beware of caching inside frameworks like Hibernate if using reader to test writing by round-tripping entities -As &amp;apos;dbo&amp;apos; you should have complete access and can circumvent permissions to access the underlying tables when required
  17. -Fixture SetUp/TearDown for static data (e.g. currencies) to satisfy foreign key constraints -Write separate helper functions (in SQL?) to add remove common data (using other schema?) -Use existing database API where possible to make tests less brittle
  18. -In T-SQL use RaiseError to throw an exception
  19. -Verifying the external API contract to detect changes (write tests instead of prototyping) -Helps to test your façade -Highly unreliable (failed automated builds causes noise) -Your UAT output can be someone else’s DEV input -Writing? (publish to bus/share)
  20. -Current system evolution from .exe stub to many components
  21. -Explain very briefly the overall architecture -Highlight where we want to provide tests -Key area of Risk –&amp;gt; the external services
  22. -Start with in-process services to enable end-to-end testing from the start -Services initially just stubs with interfaces to allow mocking
  23. -External service façade provides a mocking point, especially when external API is all concrete types -External service API tested -Internal service can be component tested with mock service -Internal service can be independently integration tested -Mocks became production components -Hubert’s colour schemes -&amp;gt; every blue box should have a purple or green box feeding it
  24. -Stubs replaced with mocks to facilitate system evolution and integration testing -Once real data can be provided through the services a realistic system test can be coded -File-system used to provide fixed set of test data for one counterparty -Mix &amp; match different mocks and fakes for different tests
  25. -Questions?