SlideShare uma empresa Scribd logo
1 de 32
Real life unit testing Dror Helper
Who am I? Software Developer Part time blogger http://blog.drorhelper.com/@dhelper One thing was the same wherever I worked…
Agenda Unit tests - what, how and why Tools of the trade How to write better tests Writing tests when it’s hard to test
Sounds familiar? Every ten bugs I fix I create a new one… I have no idea what caused that issue… I’d rather not change that function… It is impossible to unit test our project! Setting my test environment takes too long
Avoid stupid bugs
Unit tests Unit Tests Test specific functionality Clear pass/fail criteria Good unit test runs in isolation
What is a Unit Test Unit tests [TestMethod] public void CheckPassword_ValidUser_ReturnTrue() { bool result = CheckPassword(“user”, “pass”); Assert.IsTrue(result); }
What is a Unit Test Unit tests [Test] public void CheckPassword_ValidUser_ReturnTrue() { bool result = CheckPassword(“user”, “pass”); Assert.That(result, Is.True); }
The kind of test Unit tests should be: Small Atomic Test a single functional unit Isolated! Integration tests are used to test system wide functionality Unit tests
Why use automated unit tests? Quick feedback Regression Gain control of your code Unit tests
Avoid stupid bugs Unit tests
Tools of the trade Tools Server Source Control Build Server Build Script Dev Machine Test Runner Unit Testing Framework Isolation Framework Code Coverage
Continuous Integration Tools What’s new? Commit Build Server (TeamCity) There you go Start working Build artifacts We automatically got ,[object Object]
New version installer
Help files
More…Source Control (SVN) Build Agents (FinalBuilder)
Build Script at a Glance Tools
TeamCity at a Glance Tools
Development environment Make it easy to write and run tests Unit test framework Test Runner Isolation framework Know where you stand Code coverage  Tools
Unit testing frameworks NUnit xUnit MSTest Gallio (MbUnit) NBehave List of unit testing frameworks: http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks Tools
Test Runners Tools ,[object Object]
TestDrive.Net
R# TestRunner
CodeRushTestRunner
NUnitIt,[object Object]
Readability is important Why did the test fail? Avoid unnecessary debugging Understand what the test does! Write better tests
Easy to understand unit tests Names are important Don’t be afraid to repeat yourself Arrange-Act-Assert Or Act-Assert-Arrange Write better tests
Write readable tests Test only one thing (most of the time) KISS – avoid logic in your test Write better tests
Duplicate code problem Write better tests After refactoring I need to re-write my tests. Writing the same code twice is wrong
Reuse code Create objects using factories Manipulate and configure initial state Run common tests in common methods  Write better tests
Just remember	 Readability is important  - It’s still ok to repeat yourself Write better tests
Easy to run	 Running the tests takes too long Setting complicated testing environment Write better tests

Mais conteúdo relacionado

Mais procurados

Test Automation Architecture That Works by Bhupesh Dahal
Test Automation Architecture That Works by Bhupesh DahalTest Automation Architecture That Works by Bhupesh Dahal
Test Automation Architecture That Works by Bhupesh Dahal
QA or the Highway
 
An Overview of automated testing (1)
An Overview of automated testing (1)An Overview of automated testing (1)
An Overview of automated testing (1)
Rodrigo Lopes
 
Sqa, test scenarios and test cases
Sqa, test scenarios and test casesSqa, test scenarios and test cases
Sqa, test scenarios and test cases
Confiz
 
Unit testing (workshop)
Unit testing (workshop)Unit testing (workshop)
Unit testing (workshop)
Foyzul Karim
 
Moq presentation
Moq presentationMoq presentation
Moq presentation
LynxStar
 
Roy Osherove TDD From Scratch
Roy Osherove TDD From ScratchRoy Osherove TDD From Scratch
Roy Osherove TDD From Scratch
Roy Osherove
 
Test Cases Maintaining & Documenting
Test Cases Maintaining & DocumentingTest Cases Maintaining & Documenting
Test Cases Maintaining & Documenting
Seyed Ali Marjaie
 
An Introduction to unit testing
An Introduction to unit testingAn Introduction to unit testing
An Introduction to unit testing
Steven Casey
 

Mais procurados (20)

So you think you can write a test case
So you think you can write a test caseSo you think you can write a test case
So you think you can write a test case
 
Test Automation Architecture That Works by Bhupesh Dahal
Test Automation Architecture That Works by Bhupesh DahalTest Automation Architecture That Works by Bhupesh Dahal
Test Automation Architecture That Works by Bhupesh Dahal
 
How to write defect
How to write defectHow to write defect
How to write defect
 
xUnit test patterns 0
xUnit test patterns 0xUnit test patterns 0
xUnit test patterns 0
 
An Overview of automated testing (1)
An Overview of automated testing (1)An Overview of automated testing (1)
An Overview of automated testing (1)
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
 
Unit testing
Unit testingUnit testing
Unit testing
 
Automated tests
Automated testsAutomated tests
Automated tests
 
Clean code
Clean codeClean code
Clean code
 
Tdd red-green-refactor
Tdd red-green-refactorTdd red-green-refactor
Tdd red-green-refactor
 
Sqa, test scenarios and test cases
Sqa, test scenarios and test casesSqa, test scenarios and test cases
Sqa, test scenarios and test cases
 
Unit testing (workshop)
Unit testing (workshop)Unit testing (workshop)
Unit testing (workshop)
 
Php tests tips
Php tests tipsPhp tests tips
Php tests tips
 
Mock driven development using .NET
Mock driven development using .NETMock driven development using .NET
Mock driven development using .NET
 
Effective Unit Testing
Effective Unit TestingEffective Unit Testing
Effective Unit Testing
 
Moq presentation
Moq presentationMoq presentation
Moq presentation
 
Roy Osherove TDD From Scratch
Roy Osherove TDD From ScratchRoy Osherove TDD From Scratch
Roy Osherove TDD From Scratch
 
Test Cases Maintaining & Documenting
Test Cases Maintaining & DocumentingTest Cases Maintaining & Documenting
Test Cases Maintaining & Documenting
 
An Introduction to unit testing
An Introduction to unit testingAn Introduction to unit testing
An Introduction to unit testing
 
Unit testing in PHP
Unit testing in PHPUnit testing in PHP
Unit testing in PHP
 

Destaque (6)

Unit 2 unit testing
Unit 2   unit testingUnit 2   unit testing
Unit 2 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 testing, principles
Unit testing, principlesUnit testing, principles
Unit testing, principles
 
Workbook answer key unit 1
Workbook answer key   unit 1Workbook answer key   unit 1
Workbook answer key unit 1
 
American english-file-2-workbook-answers
American english-file-2-workbook-answersAmerican english-file-2-workbook-answers
American english-file-2-workbook-answers
 
El112 new headway-plus_upper_intermediate_workbook_answer_key
El112 new headway-plus_upper_intermediate_workbook_answer_keyEl112 new headway-plus_upper_intermediate_workbook_answer_key
El112 new headway-plus_upper_intermediate_workbook_answer_key
 

Semelhante a Real Life Unit Testing

Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And Mocking
Joe Wilson
 
Automated Unit Testing for Mere Mortals
Automated Unit Testing for Mere MortalsAutomated Unit Testing for Mere Mortals
Automated Unit Testing for Mere Mortals
Jess Chadwick
 

Semelhante a Real Life Unit Testing (20)

Why Unit Testingl
Why Unit TestinglWhy Unit Testingl
Why Unit Testingl
 
Why unit testingl
Why unit testinglWhy unit testingl
Why unit testingl
 
Why Unit Testingl
Why Unit TestinglWhy Unit Testingl
Why Unit Testingl
 
TDD Best Practices
TDD Best PracticesTDD Best Practices
TDD Best Practices
 
Practical unit testing tips
Practical unit testing tipsPractical unit testing tips
Practical unit testing tips
 
Unit testing
Unit testingUnit testing
Unit testing
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Unit testing basics with NUnit and Visual Studio
Unit testing basics with NUnit and Visual StudioUnit testing basics with NUnit and Visual Studio
Unit testing basics with NUnit and Visual Studio
 
An introduction to unit testing
An introduction to unit testingAn introduction to unit testing
An introduction to unit testing
 
Unit Testing, TDD and the Walking Skeleton
Unit Testing, TDD and the Walking SkeletonUnit Testing, TDD and the Walking Skeleton
Unit Testing, TDD and the Walking Skeleton
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And Mocking
 
Unit Testing and TDD 2017
Unit Testing and TDD 2017Unit Testing and TDD 2017
Unit Testing and TDD 2017
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Software Testing
Software TestingSoftware Testing
Software Testing
 
An insight to test driven development and unit testing
An insight to test driven development and unit testingAn insight to test driven development and unit testing
An insight to test driven development and unit testing
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDD
 
JavaScript Unit Testing
JavaScript Unit TestingJavaScript Unit Testing
JavaScript Unit Testing
 
An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development
 
Automated Unit Testing for Mere Mortals
Automated Unit Testing for Mere MortalsAutomated Unit Testing for Mere Mortals
Automated Unit Testing for Mere Mortals
 

Mais de Dror Helper

Mais de Dror Helper (20)

Unit testing patterns for concurrent code
Unit testing patterns for concurrent codeUnit testing patterns for concurrent code
Unit testing patterns for concurrent code
 
The secret unit testing tools no one ever told you about
The secret unit testing tools no one ever told you aboutThe secret unit testing tools no one ever told you about
The secret unit testing tools no one ever told you about
 
Debugging with visual studio beyond 'F5'
Debugging with visual studio beyond 'F5'Debugging with visual studio beyond 'F5'
Debugging with visual studio beyond 'F5'
 
From clever code to better code
From clever code to better codeFrom clever code to better code
From clever code to better code
 
From clever code to better code
From clever code to better codeFrom clever code to better code
From clever code to better code
 
A software developer guide to working with aws
A software developer guide to working with awsA software developer guide to working with aws
A software developer guide to working with aws
 
The secret unit testing tools no one has ever told you about
The secret unit testing tools no one has ever told you aboutThe secret unit testing tools no one has ever told you about
The secret unit testing tools no one has ever told you about
 
The role of the architect in agile
The role of the architect in agileThe role of the architect in agile
The role of the architect in agile
 
Harnessing the power of aws using dot net core
Harnessing the power of aws using dot net coreHarnessing the power of aws using dot net core
Harnessing the power of aws using dot net core
 
Developing multi-platform microservices using .NET core
 Developing multi-platform microservices using .NET core Developing multi-platform microservices using .NET core
Developing multi-platform microservices using .NET core
 
Harnessing the power of aws using dot net
Harnessing the power of aws using dot netHarnessing the power of aws using dot net
Harnessing the power of aws using dot net
 
Secret unit testing tools no one ever told you about
Secret unit testing tools no one ever told you aboutSecret unit testing tools no one ever told you about
Secret unit testing tools no one ever told you about
 
C++ Unit testing - the good, the bad & the ugly
C++ Unit testing - the good, the bad & the uglyC++ Unit testing - the good, the bad & the ugly
C++ Unit testing - the good, the bad & the ugly
 
Working with c++ legacy code
Working with c++ legacy codeWorking with c++ legacy code
Working with c++ legacy code
 
Visual Studio tricks every dot net developer should know
Visual Studio tricks every dot net developer should knowVisual Studio tricks every dot net developer should know
Visual Studio tricks every dot net developer should know
 
Secret unit testing tools
Secret unit testing toolsSecret unit testing tools
Secret unit testing tools
 
Electronics 101 for software developers
Electronics 101 for software developersElectronics 101 for software developers
Electronics 101 for software developers
 
Navigating the xDD Alphabet Soup
Navigating the xDD Alphabet SoupNavigating the xDD Alphabet Soup
Navigating the xDD Alphabet Soup
 
Building unit tests correctly
Building unit tests correctlyBuilding unit tests correctly
Building unit tests correctly
 
Who’s afraid of WinDbg
Who’s afraid of WinDbgWho’s afraid of WinDbg
Who’s afraid of WinDbg
 

Último

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Último (20)

Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 

Real Life Unit Testing

  • 1. Real life unit testing Dror Helper
  • 2. Who am I? Software Developer Part time blogger http://blog.drorhelper.com/@dhelper One thing was the same wherever I worked…
  • 3. Agenda Unit tests - what, how and why Tools of the trade How to write better tests Writing tests when it’s hard to test
  • 4. Sounds familiar? Every ten bugs I fix I create a new one… I have no idea what caused that issue… I’d rather not change that function… It is impossible to unit test our project! Setting my test environment takes too long
  • 6. Unit tests Unit Tests Test specific functionality Clear pass/fail criteria Good unit test runs in isolation
  • 7. What is a Unit Test Unit tests [TestMethod] public void CheckPassword_ValidUser_ReturnTrue() { bool result = CheckPassword(“user”, “pass”); Assert.IsTrue(result); }
  • 8. What is a Unit Test Unit tests [Test] public void CheckPassword_ValidUser_ReturnTrue() { bool result = CheckPassword(“user”, “pass”); Assert.That(result, Is.True); }
  • 9. The kind of test Unit tests should be: Small Atomic Test a single functional unit Isolated! Integration tests are used to test system wide functionality Unit tests
  • 10. Why use automated unit tests? Quick feedback Regression Gain control of your code Unit tests
  • 11. Avoid stupid bugs Unit tests
  • 12. Tools of the trade Tools Server Source Control Build Server Build Script Dev Machine Test Runner Unit Testing Framework Isolation Framework Code Coverage
  • 13.
  • 16. More…Source Control (SVN) Build Agents (FinalBuilder)
  • 17. Build Script at a Glance Tools
  • 18. TeamCity at a Glance Tools
  • 19. Development environment Make it easy to write and run tests Unit test framework Test Runner Isolation framework Know where you stand Code coverage Tools
  • 20. Unit testing frameworks NUnit xUnit MSTest Gallio (MbUnit) NBehave List of unit testing frameworks: http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks Tools
  • 21.
  • 25.
  • 26. Readability is important Why did the test fail? Avoid unnecessary debugging Understand what the test does! Write better tests
  • 27. Easy to understand unit tests Names are important Don’t be afraid to repeat yourself Arrange-Act-Assert Or Act-Assert-Arrange Write better tests
  • 28. Write readable tests Test only one thing (most of the time) KISS – avoid logic in your test Write better tests
  • 29. Duplicate code problem Write better tests After refactoring I need to re-write my tests. Writing the same code twice is wrong
  • 30. Reuse code Create objects using factories Manipulate and configure initial state Run common tests in common methods Write better tests
  • 31. Just remember Readability is important - It’s still ok to repeat yourself Write better tests
  • 32. Easy to run Running the tests takes too long Setting complicated testing environment Write better tests
  • 33. Test should be easy to run Integration vs. Unit tests Simple configuration Use fakes Write better tests
  • 34. Fragile tests Tests fail every time I change my code Write better tests
  • 35. Avoid over specification Write better tests Don’t test private/internal (most of the time) Fake as little as necessary Test only one thing (most of the time)
  • 36. Testing the un-testable First - better understand the problem Isolation framework Design/refactor for Testability Reflection Use integration tests Testing un-testable
  • 37. Unit tests = avoid stupid bugs
  • 38. Resources This Week In Test webcast www.typemock.com/this-week-in-test/ Book: The art of unit testing Typemock insider’s blog: blog.typemock.com Follow us on twitter @typemock

Notas do Editor

  1. Software project should not be a ticking bomb.
  2. AtomicRepeatableNo dependency between tests/test orderClear pass/fail criteria
  3. The backbone of TDD (and development)Show how CI & script runner run the test suite – perhaps publish results to web