SlideShare a Scribd company logo
Unit Testing
François Camus
Advantages
 Quick feedback loop
 Run all tests to know if something is broken
 Less need to run application to debug production code
 Can debug from test method instead
 Provides confidence
 Refactoring
 Releasing more often
 Documents production code behaviour
 Serves as production code safety net
 Avoids Change And Pray syndrome (CPD)
Test Project
 Contains tests for a specific project
 Naming convention
 To avoid confusion
 e.g. when browsing files
 e.g. when having lots of tabs
 Library namespace
 e.g. OpenControl.Web.Mappers
 Library Test Project namespace
 e.g. OpenControl.Web.Mappers.Tests
 Test Class name
 e.g. ModelMapperTests
 Test project to follow same structure as project being
tested
Unit Testing frameworks
 Microsoft Test framework 
 NUnit
 xUnit.net
 http://xunit.codeplex.com/wikipage?title=Comparisons
Test Execution Lifecycle
 Initialization (before each test execution)
 initialises dependencies and fields that are common to many tests
 no setup here: each test does its own setup
 Easier to maintain test code
 Don’t want spaghetti test code all depending on a common setup method
 Execution
 Does local test setup
 Calls method to test passing parameters and storing result if any
 Asserts something on result or behaviour
 Cleanup (after each test execution)
 Dispose of initialized dependencies
 Test Class initialisation and cleanup
 Usually used by custom testing framework
 e.g. Tester base class containing test helpers
 Initialises before first test execution
 Cleans up after last test execution
Test method naming convention
[Fact]
public void Method_Condition_Result() {}
1. Method being tested
2. Condition to meet test result (optional)
3. Test result
[Fact]
public void GetData_DataExists_ReturnsData() {}
Test method structure
 AAA: Arrange/Act/Assert
1. Arrange: local test setup
2. Act: calls method to test
3. Assert: asserts result or behaviour
 Common structure to all tests
 Easier for other developers to maintain code
 Helpful when doing Test Driven Development
Interaction based Unit Testing
 Asserting expectation on method
 Couples to internal implementation
 Less flexible to do refactoring
 Leads to fragile tests
 Easier when starting unit testing
 Unavoidable
 Need them when unable to only test result
 e.g Testing that a void method was called
 About 10% of tests
 http://programmaticallyspeaking.com/?p=713
State based Unit Testing
 Tests method result rather than expectation
 Easier to maintain
 Only local test setup may need amendment
 Preferred form of unit testing
Testing internals
 Add attribute to production code assembly info to make
internals visible to specific library
[assembly: InternalsVisibleTo("OpenControl.ClassLibrary.Tests")]
 http://ayende.com/Wiki/Rhino+Mocks+-
+Internal+Methods.ashx
One assert per test
 Atomic
 Easier to understand
 Easier to maintain
 When refactoring production code, you get better
feedback on what breaks
Dependency Injection
 Constructor parameter injection
 Property injection
Mocking
 Mocking frameworks (all based on Castle Dynamic Proxy)
 http://www.castleproject.org/projects/dynamicproxy/
 Add attribute to production code assembly info:
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]
 NSubstitute
 Shorter but awkward syntax
 Less lambda so changing developers’ habits
 Rhino Mocks
 Too big (“Does the coffee !” Manu)
 Confusion for new users on which syntax to use
 Latest syntax is following state based unit testing approach
 But old syntax remains
 Large community
 Moq
 Smaller and simpler
 Large community
 http://code.google.com/p/moq/wiki/QuickStart
Tools
 Test Driven .Net (visual studio extension)
 Works with xUnit.net (may need to run xunit installer to add
support to Test Driven .Net)
 ReSharper...
 Jenkins
 After build runs tests via plugin (xUnit plugin...)
 Need to upgrade Jenkins to support .NET 4+
Conclusion
 Questions ?
 TDD next time

More Related Content

What's hot

Unit Tests And Automated Testing
Unit Tests And Automated TestingUnit Tests And Automated Testing
Unit Tests And Automated Testing
Lee Englestone
 
Software Quality via Unit Testing
Software Quality via Unit TestingSoftware Quality via Unit Testing
Software Quality via Unit Testing
Shaun Abram
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
Anuj Arora
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
Sergey Podolsky
 
Test driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + EclipseTest driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + Eclipse
UTC Fire & Security
 
Unit Test Presentation
Unit Test PresentationUnit Test Presentation
Unit Test PresentationSayedur Rahman
 
Clean Unit Test Patterns
Clean Unit Test PatternsClean Unit Test Patterns
Clean Unit Test Patterns
Frank Appel
 
Beginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NETBeginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NET
Baskar K
 
Benefit From Unit Testing In The Real World
Benefit From Unit Testing In The Real WorldBenefit From Unit Testing In The Real World
Benefit From Unit Testing In The Real WorldDror Helper
 
Unit test
Unit testUnit test
Unit test
Tran Duc
 
Unit Testing Guidelines
Unit Testing GuidelinesUnit Testing Guidelines
Unit Testing GuidelinesJoel Hooks
 
Unit Testing Fundamentals
Unit Testing FundamentalsUnit Testing Fundamentals
Unit Testing FundamentalsRichard Paul
 
Unit testing
Unit testingUnit testing
Unit testing
princezzlove
 
Unit Testing (C#)
Unit Testing (C#)Unit Testing (C#)
Unit Testing (C#)
Prashant Cholachagudd
 
UNIT TESTING
UNIT TESTINGUNIT TESTING
UNIT TESTING
Marius Crisan
 
Moq presentation
Moq presentationMoq presentation
Moq presentationLynxStar
 
Unit Testing 101
Unit Testing 101Unit Testing 101
Unit Testing 101
Dave Bouwman
 
Unit Test + Functional Programming = Love
Unit Test + Functional Programming = LoveUnit Test + Functional Programming = Love
Unit Test + Functional Programming = Love
Alvaro Videla
 
Unit Testing Done Right
Unit Testing Done RightUnit Testing Done Right
Unit Testing Done Right
Brian Fenton
 

What's hot (20)

Unit Tests And Automated Testing
Unit Tests And Automated TestingUnit Tests And Automated Testing
Unit Tests And Automated Testing
 
Software Quality via Unit Testing
Software Quality via Unit TestingSoftware Quality via Unit Testing
Software Quality via Unit Testing
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Test driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + EclipseTest driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + Eclipse
 
Unit Test Presentation
Unit Test PresentationUnit Test Presentation
Unit Test Presentation
 
Unit testing
Unit testingUnit testing
Unit testing
 
Clean Unit Test Patterns
Clean Unit Test PatternsClean Unit Test Patterns
Clean Unit Test Patterns
 
Beginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NETBeginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NET
 
Benefit From Unit Testing In The Real World
Benefit From Unit Testing In The Real WorldBenefit From Unit Testing In The Real World
Benefit From Unit Testing In The Real World
 
Unit test
Unit testUnit test
Unit test
 
Unit Testing Guidelines
Unit Testing GuidelinesUnit Testing Guidelines
Unit Testing Guidelines
 
Unit Testing Fundamentals
Unit Testing FundamentalsUnit Testing Fundamentals
Unit Testing Fundamentals
 
Unit testing
Unit testingUnit testing
Unit testing
 
Unit Testing (C#)
Unit Testing (C#)Unit Testing (C#)
Unit Testing (C#)
 
UNIT TESTING
UNIT TESTINGUNIT TESTING
UNIT TESTING
 
Moq presentation
Moq presentationMoq presentation
Moq presentation
 
Unit Testing 101
Unit Testing 101Unit Testing 101
Unit Testing 101
 
Unit Test + Functional Programming = Love
Unit Test + Functional Programming = LoveUnit Test + Functional Programming = Love
Unit Test + Functional Programming = Love
 
Unit Testing Done Right
Unit Testing Done RightUnit Testing Done Right
Unit Testing Done Right
 

Similar to Unit Testing

Into The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applicationsInto The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applications
Ortus Solutions, Corp
 
03 test specification and execution
03   test specification and execution03   test specification and execution
03 test specification and execution
Clemens Reijnen
 
Automated Testing Of EPiServer CMS Sites
Automated Testing Of EPiServer CMS SitesAutomated Testing Of EPiServer CMS Sites
Automated Testing Of EPiServer CMS Sitesjoelabrahamsson
 
Building Maintainable Android Apps (DroidCon NYC 2014)
Building Maintainable Android Apps (DroidCon NYC 2014)Building Maintainable Android Apps (DroidCon NYC 2014)
Building Maintainable Android Apps (DroidCon NYC 2014)
Kevin Schultz
 
Testing Java Web Apps With Selenium
Testing Java Web Apps With SeleniumTesting Java Web Apps With Selenium
Testing Java Web Apps With Selenium
Marakana Inc.
 
Unit testing php-unit - phing - selenium_v2
Unit testing   php-unit - phing - selenium_v2Unit testing   php-unit - phing - selenium_v2
Unit testing php-unit - phing - selenium_v2Tricode (part of Dept)
 
Stopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under TestStopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under Test
Seb Rose
 
justin presentation upload PPT june 19
justin presentation upload PPT june 19justin presentation upload PPT june 19
justin presentation upload PPT june 19techweb08
 
justin for ppt1 by browse button
justin for ppt1 by browse buttonjustin for ppt1 by browse button
justin for ppt1 by browse buttontechweb08
 
alka ppt test from13
alka ppt test from13 alka ppt test from13
alka ppt test from13 techweb08
 
alka ppt upload no code change
alka ppt upload no code changealka ppt upload no code change
alka ppt upload no code changetechweb08
 
justin presentation slideshare1
justin presentation slideshare1justin presentation slideshare1
justin presentation slideshare1techweb08
 
justin presentation upload PPT june 25 ADVANCED
justin presentation upload PPT june 25 ADVANCEDjustin presentation upload PPT june 25 ADVANCED
justin presentation upload PPT june 25 ADVANCEDtechweb08
 
upload ppt by browse button
upload ppt by browse buttonupload ppt by browse button
upload ppt by browse buttontechweb08
 
upload ppt by browse button
upload ppt by browse buttonupload ppt by browse button
upload ppt by browse buttontechweb08
 
Paper PsUpload
Paper PsUploadPaper PsUpload
Paper PsUploadtechweb08
 
justin presentation Slideshare PPT upload June 25 Final one
justin presentation Slideshare PPT upload June 25 Final onejustin presentation Slideshare PPT upload June 25 Final one
justin presentation Slideshare PPT upload June 25 Final onetechweb08
 

Similar to Unit Testing (20)

Into The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applicationsInto The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applications
 
03 test specification and execution
03   test specification and execution03   test specification and execution
03 test specification and execution
 
Automated Testing Of EPiServer CMS Sites
Automated Testing Of EPiServer CMS SitesAutomated Testing Of EPiServer CMS Sites
Automated Testing Of EPiServer CMS Sites
 
Building Maintainable Android Apps (DroidCon NYC 2014)
Building Maintainable Android Apps (DroidCon NYC 2014)Building Maintainable Android Apps (DroidCon NYC 2014)
Building Maintainable Android Apps (DroidCon NYC 2014)
 
Testing Java Web Apps With Selenium
Testing Java Web Apps With SeleniumTesting Java Web Apps With Selenium
Testing Java Web Apps With Selenium
 
Unit testing php-unit - phing - selenium_v2
Unit testing   php-unit - phing - selenium_v2Unit testing   php-unit - phing - selenium_v2
Unit testing php-unit - phing - selenium_v2
 
Stopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under TestStopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under Test
 
Paper CS
Paper CSPaper CS
Paper CS
 
alkatest7
alkatest7alkatest7
alkatest7
 
justin presentation upload PPT june 19
justin presentation upload PPT june 19justin presentation upload PPT june 19
justin presentation upload PPT june 19
 
justin for ppt1 by browse button
justin for ppt1 by browse buttonjustin for ppt1 by browse button
justin for ppt1 by browse button
 
alka ppt test from13
alka ppt test from13 alka ppt test from13
alka ppt test from13
 
alka ppt upload no code change
alka ppt upload no code changealka ppt upload no code change
alka ppt upload no code change
 
justin presentation slideshare1
justin presentation slideshare1justin presentation slideshare1
justin presentation slideshare1
 
justin presentation upload PPT june 25 ADVANCED
justin presentation upload PPT june 25 ADVANCEDjustin presentation upload PPT june 25 ADVANCED
justin presentation upload PPT june 25 ADVANCED
 
upload ppt by browse button
upload ppt by browse buttonupload ppt by browse button
upload ppt by browse button
 
Paper Ps
Paper PsPaper Ps
Paper Ps
 
upload ppt by browse button
upload ppt by browse buttonupload ppt by browse button
upload ppt by browse button
 
Paper PsUpload
Paper PsUploadPaper PsUpload
Paper PsUpload
 
justin presentation Slideshare PPT upload June 25 Final one
justin presentation Slideshare PPT upload June 25 Final onejustin presentation Slideshare PPT upload June 25 Final one
justin presentation Slideshare PPT upload June 25 Final one
 

Recently uploaded

FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
Fwdays
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 

Recently uploaded (20)

FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 

Unit Testing

  • 2. Advantages  Quick feedback loop  Run all tests to know if something is broken  Less need to run application to debug production code  Can debug from test method instead  Provides confidence  Refactoring  Releasing more often  Documents production code behaviour  Serves as production code safety net  Avoids Change And Pray syndrome (CPD)
  • 3. Test Project  Contains tests for a specific project  Naming convention  To avoid confusion  e.g. when browsing files  e.g. when having lots of tabs  Library namespace  e.g. OpenControl.Web.Mappers  Library Test Project namespace  e.g. OpenControl.Web.Mappers.Tests  Test Class name  e.g. ModelMapperTests  Test project to follow same structure as project being tested
  • 4. Unit Testing frameworks  Microsoft Test framework   NUnit  xUnit.net  http://xunit.codeplex.com/wikipage?title=Comparisons
  • 5. Test Execution Lifecycle  Initialization (before each test execution)  initialises dependencies and fields that are common to many tests  no setup here: each test does its own setup  Easier to maintain test code  Don’t want spaghetti test code all depending on a common setup method  Execution  Does local test setup  Calls method to test passing parameters and storing result if any  Asserts something on result or behaviour  Cleanup (after each test execution)  Dispose of initialized dependencies  Test Class initialisation and cleanup  Usually used by custom testing framework  e.g. Tester base class containing test helpers  Initialises before first test execution  Cleans up after last test execution
  • 6. Test method naming convention [Fact] public void Method_Condition_Result() {} 1. Method being tested 2. Condition to meet test result (optional) 3. Test result [Fact] public void GetData_DataExists_ReturnsData() {}
  • 7. Test method structure  AAA: Arrange/Act/Assert 1. Arrange: local test setup 2. Act: calls method to test 3. Assert: asserts result or behaviour  Common structure to all tests  Easier for other developers to maintain code  Helpful when doing Test Driven Development
  • 8. Interaction based Unit Testing  Asserting expectation on method  Couples to internal implementation  Less flexible to do refactoring  Leads to fragile tests  Easier when starting unit testing  Unavoidable  Need them when unable to only test result  e.g Testing that a void method was called  About 10% of tests  http://programmaticallyspeaking.com/?p=713
  • 9. State based Unit Testing  Tests method result rather than expectation  Easier to maintain  Only local test setup may need amendment  Preferred form of unit testing
  • 10. Testing internals  Add attribute to production code assembly info to make internals visible to specific library [assembly: InternalsVisibleTo("OpenControl.ClassLibrary.Tests")]  http://ayende.com/Wiki/Rhino+Mocks+- +Internal+Methods.ashx
  • 11. One assert per test  Atomic  Easier to understand  Easier to maintain  When refactoring production code, you get better feedback on what breaks
  • 12. Dependency Injection  Constructor parameter injection  Property injection
  • 13. Mocking  Mocking frameworks (all based on Castle Dynamic Proxy)  http://www.castleproject.org/projects/dynamicproxy/  Add attribute to production code assembly info: [assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]  NSubstitute  Shorter but awkward syntax  Less lambda so changing developers’ habits  Rhino Mocks  Too big (“Does the coffee !” Manu)  Confusion for new users on which syntax to use  Latest syntax is following state based unit testing approach  But old syntax remains  Large community  Moq  Smaller and simpler  Large community  http://code.google.com/p/moq/wiki/QuickStart
  • 14. Tools  Test Driven .Net (visual studio extension)  Works with xUnit.net (may need to run xunit installer to add support to Test Driven .Net)  ReSharper...  Jenkins  After build runs tests via plugin (xUnit plugin...)  Need to upgrade Jenkins to support .NET 4+