Real life unit testingDror Helper
Who am I?Software DeveloperPart time blogger http://blog.drorhelper.com/@dhelperOne thing was the same wherever I worked…
AgendaUnit tests - what, how and whyTools of the tradeHow to write better testsWriting 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 testsUnit TestsTest specific functionalityClear pass/fail criteriaGood unit test runs in isolation
What is a Unit TestUnit tests[TestMethod]public void CheckPassword_ValidUser_ReturnTrue(){bool result = CheckPassword(“user”, “pass”);Assert.IsTrue(result);}
What is a Unit TestUnit tests[Test]public void CheckPassword_ValidUser_ReturnTrue(){bool result = CheckPassword(“user”, “pass”);Assert.That(result, Is.True);}
The kind of testUnit tests should be:SmallAtomicTest a single functional unitIsolated!Integration tests are used to test system wide functionalityUnit tests
Why use automated unit tests?Quick feedbackRegressionGain control of your codeUnit tests
Avoid stupid bugsUnit tests
Tools of the tradeToolsServerSource ControlBuild ServerBuild ScriptDev MachineTest RunnerUnit Testing FrameworkIsolation FrameworkCode Coverage
Continuous IntegrationToolsWhat’s new?CommitBuild Server(TeamCity)There you goStart workingBuild artifactsWe automatically gotError reports & logs
New version installer
Help files
More…Source Control(SVN)Build Agents(FinalBuilder)
Build Script at a GlanceTools
TeamCity at a GlanceTools
Development environmentMake it easy to write and run testsUnit test frameworkTest RunnerIsolation frameworkKnow where you standCode coverage Tools
Unit testing frameworksNUnitxUnitMSTestGallio (MbUnit)NBehaveList of unit testing frameworks: http://en.wikipedia.org/wiki/List_of_unit_testing_frameworksTools
Test RunnersToolsVisual Studio (MSTest)
TestDrive.Net
R# TestRunner
CodeRushTestRunner
NUnitItHow to write better testsWrite better testsWrite easy to understand testsReuse code where appropriateRunning test should be easyAvoid fragile testsTrust Your Tests!
Readability is importantWhy did the test fail?Avoid unnecessary debuggingUnderstand what the test does!Write better tests
Easy to understand unit testsNames are importantDon’t be afraid to repeat yourselfArrange-Act-AssertOr Act-Assert-ArrangeWrite better tests
Write readable testsTest only one thing (most of the time)KISS – avoid logic in your testWrite better tests
Duplicate code problemWrite better testsAfter refactoring I need to re-write my tests.Writing the same code twice is wrong
Reuse codeCreate objects using factoriesManipulate and configure initial stateRun common tests in common methods Write better tests
Just remember	Readability is important - It’s still ok to repeat yourselfWrite better tests
Easy to run	Running the tests takes too longSetting complicated testing environmentWrite better tests

Real Life Unit Testing

Editor's Notes

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