SlideShare ist ein Scribd-Unternehmen logo
1 von 15
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

Weitere ähnliche Inhalte

Was ist angesagt?

Unit Tests And Automated Testing
Unit Tests And Automated TestingUnit Tests And Automated Testing
Unit Tests And Automated TestingLee Englestone
 
Software Quality via Unit Testing
Software Quality via Unit TestingSoftware Quality via Unit Testing
Software Quality via Unit TestingShaun Abram
 
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 + EclipseUTC 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 PatternsFrank 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 .NETBaskar 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 Testing Guidelines
Unit Testing GuidelinesUnit Testing Guidelines
Unit Testing GuidelinesJoel Hooks
 
Unit Testing Fundamentals
Unit Testing FundamentalsUnit Testing Fundamentals
Unit Testing FundamentalsRichard Paul
 
Moq presentation
Moq presentationMoq presentation
Moq presentationLynxStar
 
Unit Test + Functional Programming = Love
Unit Test + Functional Programming = LoveUnit Test + Functional Programming = Love
Unit Test + Functional Programming = LoveAlvaro Videla
 
Unit Testing Done Right
Unit Testing Done RightUnit Testing Done Right
Unit Testing Done RightBrian Fenton
 

Was ist angesagt? (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
 

Ähnlich wie Unit Testing Guide - Quick Feedback, Refactoring Confidence

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 applicationsOrtus Solutions, Corp
 
03 test specification and execution
03   test specification and execution03   test specification and execution
03 test specification and executionClemens 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 SeleniumMarakana 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 TestSeb 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
 
upload ppt by browse button
upload ppt by browse buttonupload ppt by browse button
upload ppt by browse buttontechweb08
 
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
 
upload ppt1 by browse button
upload ppt1 by browse buttonupload ppt1 by browse button
upload ppt1 by browse buttontechweb08
 
upload ppt by browse button
upload ppt by browse buttonupload ppt by browse button
upload ppt by browse buttontechweb08
 
Justin Presentation PPT Upload June 25 adv
Justin Presentation PPT Upload June 25 advJustin Presentation PPT Upload June 25 adv
Justin Presentation PPT Upload June 25 advtechweb08
 
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
 
justin presentation slideshare1
justin presentation slideshare1justin presentation slideshare1
justin presentation slideshare1techweb08
 

Ähnlich wie Unit Testing Guide - Quick Feedback, Refactoring Confidence (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
 
upload ppt by browse button
upload ppt by browse buttonupload ppt by browse button
upload ppt by browse button
 
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
 
upload ppt1 by browse button
upload ppt1 by browse buttonupload ppt1 by browse button
upload ppt1 by browse button
 
Paper Ps
Paper PsPaper Ps
Paper Ps
 
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
 
Justin Presentation PPT Upload June 25 adv
Justin Presentation PPT Upload June 25 advJustin Presentation PPT Upload June 25 adv
Justin Presentation PPT Upload June 25 adv
 
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
 
justin presentation slideshare1
justin presentation slideshare1justin presentation slideshare1
justin presentation slideshare1
 

Kürzlich hochgeladen

Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 

Kürzlich hochgeladen (20)

Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 

Unit Testing Guide - Quick Feedback, Refactoring Confidence

  • 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+