1 / 32

Real life unit testing

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.

ava-norman
Download Presentation

Real life unit testing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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

  5. Avoid stupid bugs

  6. Unit tests Unit Tests • Test specific functionality • Clear pass/fail criteria • Good unit test runs in isolation

  7. Unit tests What is a Unit Test [TestMethod] public void CheckPassword_ValidUser_ReturnTrue() { bool result = CheckPassword(“user”, “pass”); Assert.IsTrue(result); }

  8. Unit tests What is a Unit Test [Test] public void CheckPassword_ValidUser_ReturnTrue() { bool result = CheckPassword(“user”, “pass”); Assert.That(result, Is.True); }

  9. Unit tests 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

  10. Unit tests Why use automated unit tests? • Quick feedback • Regression • Gain control of your code

  11. Unit tests Avoid stupid bugs

  12. Tools Tools of the trade Server Build Server Source Control Build Script Dev Machine Unit Testing Framework Test Runner Isolation Framework Code Coverage

  13. Tools Continuous Integration What’s new? Commit Build Server (TeamCity) There you go Start working Build artifacts • We automatically got • Error reports & logs • New version installer • Help files • More… Source Control (SVN) Build Agents (FinalBuilder)

  14. Tools Build Script at a Glance

  15. Tools TeamCity at a Glance

  16. Tools Development environment • Make it easy to write and run tests • Unit test framework • Test Runner • Isolation framework • Know where you stand • Code coverage

  17. 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

  18. Tools Test Runners • Visual Studio (MSTest) • TestDrive.Net • R# TestRunner • CodeRushTestRunner • NUnitIt

  19. Write better tests How to write better tests • Write easy to understand tests • Reuse code where appropriate • Running test should be easy • Avoid fragile tests Trust Your Tests!

  20. Write better tests Readability is important • Why did the test fail? • Avoid unnecessary debugging • Understand what the test does!

  21. 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

  22. Write better tests Write readable tests • Test only one thing (most of the time) • KISS – avoid logic in your test

  23. Write better tests Duplicate code problem • After refactoring I need to re-write my tests. • Writing the same code twice is wrong

  24. Write better tests Reuse code • Create objects using factories • Manipulate and configure initial state • Run common tests in common methods

  25. Write better tests Just remember Readability is important - It’s still ok to repeat yourself

  26. Write better tests Easy to run • Running the tests takes too long • Setting complicated testing environment

  27. Write better tests Test should be easy to run • Integration vs. Unit tests • Simple configuration • Use fakes

  28. Write better tests Fragile tests • Tests fail every time I change my code

  29. Write better tests Avoid over specification • Don’t test private/internal (most of the time) • Fake as little as necessary • Test only one thing (most of the time)

  30. Testing un-testable Testing the un-testable • First - better understand the problem • Isolation framework • Design/refactor for Testability • Reflection • Use integration tests

  31. Unit tests = avoid stupid bugs

  32. 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

More Related