![Page 1: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/1.jpg)
Effective Unit Testing
Brad Wilson
Scott Densmore
![Page 2: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/2.jpg)
Agenda
• What is Unit Testing?
• Typical Unit Testing Problems
• Best Practices for Effective Unit Testing
![Page 3: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/3.jpg)
What is Unit Testing?
![Page 4: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/4.jpg)
A Definition
• Unit Testing is code that…
– Is written by developers, for developers.
– Exercises a small, specific area of functionality.
– Helps “prove” that a piece of code does what the developer expects it to do.
![Page 5: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/5.jpg)
What Unit Testing Is Not
• Acceptance Testing (aka Functional Testing)
• Performance Testing
• Scalability Testing
![Page 6: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/6.jpg)
Marick’s Four Quadrants of Testing
Customer
Tests
Programmer
Tests
Exploratory
Tests
Scalability
Performance
Usability
Security
Business Facing
Technology Facing
Su
pp
ort
De
ve
lop
me
nt
Critiq
ue
Re
su
lt
Source: Brian Marick – http://testing.com/
![Page 7: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/7.jpg)
Why Unit Test
• It will make your life easier
• Better code
• Better designs
• Code is easier to maintain later
• Confidence when you code
![Page 8: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/8.jpg)
Common Excuses
• I’m not a tester!
• It takes too much time.
• It takes too long to run the tests.
• I don’t know how to test it.
• I don’t really know what it is supposed to do, so I can’t test it.
• But it compiles! It doesn’t need tests.
![Page 9: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/9.jpg)
Typical Unit Testing Problems
![Page 10: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/10.jpg)
Testing Is Monotonous and Boring
• Often indicates bad tooling or bad techniques
• Unit testing should…
– Be easy to do
– Provide rapid feedback
![Page 11: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/11.jpg)
Poor Test Coverage
• Often happens when tests not written first
• Hard to retrofit later
• Test coverage is orthogonal to test quality
![Page 12: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/12.jpg)
Purpose of Tests is Misunderstood
• Remember why we do unit testing
• Easy to get distracted by irrelevant things
• What is the scope of the test?
![Page 13: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/13.jpg)
Informal Testing Process
• How does your team do unit testing?
– Test-first?
– Intuitive “poking and prodding” style testing?
– Smoke testing?
– Whatever the developer wants?
![Page 14: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/14.jpg)
Inconsistent Testing
• Are tests required before check in?
• How is it enforced?
![Page 15: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/15.jpg)
Low Test Quality
• Single biggest problem in unit testing today
• Testing requires a special mindset
• Tools only solve half the problem
![Page 16: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/16.jpg)
Code Not Designed for Test
• Often the cause of low test quality
• Hard to design testable code
• However, testable code often is better designed
![Page 17: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/17.jpg)
Tests Not Maintained
• So you’ve shipped your code and someone finds a bug…
• Do you fix the tests? Or just fix the bug?
• What about the rest of the team?
![Page 18: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/18.jpg)
Best Practices forEffective Unit Testing
![Page 19: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/19.jpg)
Automate Your Tests
• If it is hard to run tests, you won’t do it
• Manual (aka scripted) tests make regression testing very hard
![Page 20: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/20.jpg)
Use Good Tools
• The tools should make it…
– Easy to write tests
– Easy to organize your tests
– Easy to run tests (all, some, one)
• They should provide…
– Quick feedback of pass/fail
– Details on the fail conditions
![Page 21: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/21.jpg)
Use Good Tools
Testing Focused Tools
• NUnit and/or MSTest
• Test Driven .NET
• NMock
Supporting Tools
• MSBuild
• Resharper
• CodeRush
• CodeSmith
![Page 22: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/22.jpg)
Get a Mentor
• Testing is as much an art as a science
• Learning to write good tests is hard
• Some people are naturally good at it and some aren’t
![Page 23: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/23.jpg)
Use a Test List
• Start your development activities by writing down a list of things you want to test
• You will often think of a test while writing another one. When you do, add it to the list.
• Review your list frequently
![Page 24: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/24.jpg)
Write Tests First
• Test-driven development (TDD) is a proven way of improving quality*
• TDD’s main objective is not testing software! (this is a side effect)
• TDD’s main objective is to aid programmers and customers during the development process with unambiguous requirements.
• Code is written with testing as a primary motivation. In short, the code is testable.
* http://collaboration.csc.ncsu.edu/laurie/Papers/TDDpaperv8.pdf
![Page 25: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/25.jpg)
What to Test
• The “did we get what we expected” test isn’t always good enough
• Ask yourself the question…
– “If the code ran correctly, how would I know?”
![Page 26: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/26.jpg)
What to Test - Testing Heuristics
• Test at the boundaries
• Test every error message
• Test different configurations
• Run tests that are annoying to setup
• Avoid redundant tests
– Source: Lessons Learned in Software Testing,Cem Kaner, James Bach, Brett Pettichord
![Page 27: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/27.jpg)
What to Test – BICEP
B Are the boundary conditions correct?
I Can you check inverse relationships?
C Can you cross check using other means?
E Can you force an error condition to happen?
P Are the performance characteristics acceptable?
Source:Pragmatic Unit Testing in C# with NUnit, Andy Hunt and Dave Thomas
![Page 28: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/28.jpg)
Test Structure
• William Wake’s 3-A Pattern
– Arrange
– Act
– Assert
![Page 29: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/29.jpg)
Designing for Test
• One simple question to help you write good code…
“How am I going to test this?”
• If you don’t know the answer, then you probably need to reconsider your design.
![Page 30: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/30.jpg)
What is a Mock Object?
“A mock object is an object created to stand in for an object that your code will be collaborating with. Your code can call methods on the mock object, which will deliver results as set up by your tests.”
Source: JUnit in Action, Vincent Massol
![Page 31: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/31.jpg)
When are mocks appropriate?
• Real object has non-deterministic behavior
• Real object is difficult to set up
• Real object has behavior that is difficult to cause
• Real object is slow
Source:
http://c2.com/cgi/wiki?MockObjects
![Page 32: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/32.jpg)
Mock Downsides
• Code complexity of being able to switch between real and mock implementations
• Maintaining the mock
![Page 33: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/33.jpg)
Properties of Good Tests
• Automatic Tests need to be run checkedautomatically
• Thorough Test everything that could possiblybreak
• Repeatable Tests should produce the same resultseach time they are run
• Independent A test should exercise only one thingat a time
• Professional Tests are code too! Write themprofessionally.
Source: Pragmatic Unit Testing in C# with NUnit
![Page 34: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/34.jpg)
Recommended Test Organization
• Tests in a separate project/assembly• Naming conventions
If you are testing the class:Foo.Bar.Baz
Then you should name your testsFoo.Bar.Tests.BazTests
orFoo.Bar.Tests.BazFixture
• Note the namespace and test class name!
![Page 35: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/35.jpg)
Naming Your Tests
• The test’s name should describe the desired outcome and not just be the name of the method under test
Good name: PopReturnsLastPushedItem
Bad name: PopTest
![Page 36: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/36.jpg)
When to Run Tests
• When you write a new method…… compile and run local unit tests
• When you fix a bug…… run the test that illustrates that bug.
• Any successful compile…… run local unit tests.
• Before you check in…… run all tests.
• Continuously…... check out and build the project from scratch including all
unit tests.
![Page 37: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/37.jpg)
Do not check in code that…
• Is incomplete (e.g. missing dependencies)
• Doesn’t compile
• Compiles but breaks other code
• Doesn’t have unit tests
• Has failing unit tests
• Passes its tests but causes other tests to fail.
• Has tests with the [Ignore] attribute
![Page 38: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/38.jpg)
Additional Resources
![Page 39: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/39.jpg)
Web Sites
• http://www.testdriven.com
• http://www.xprogramming.com
• http://workspaces.gotdotnet.com/tdd
![Page 40: Effective Unit Testing with NUnit - Brad Wilson · A Definition •Unit Testing is code that… –Is written by developers, for developers. –Exercises a small, specific area of](https://reader030.vdocument.in/reader030/viewer/2022040110/5d518ede88c993c6748b459d/html5/thumbnails/40.jpg)
Books
• The Pragmatic ProgrammerAndy Hunt and Dave Thomas
• Test-Driven Development in Microsoft .NETJim Newkirk and Alexei Vorontsov
• Test-Driven Development, by ExampleKent Beck
• Pragmatic Unit Testing in C# with NUnitAndy Hunt and Dave Thomas
• Working Effectively With Legacy CodeMichael Feathers
• RefactoringMartin Fowler
• Lessons Learned in Software TestingCem Kaner, James Bach, and Brett Pettichord