320 likes | 451 Views
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.
E N D
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
Unit tests Unit Tests • Test specific functionality • Clear pass/fail criteria • Good unit test runs in isolation
Unit tests What is a Unit Test [TestMethod] public void CheckPassword_ValidUser_ReturnTrue() { bool result = CheckPassword(“user”, “pass”); Assert.IsTrue(result); }
Unit tests What is a Unit Test [Test] public void CheckPassword_ValidUser_ReturnTrue() { bool result = CheckPassword(“user”, “pass”); Assert.That(result, Is.True); }
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
Unit tests Why use automated unit tests? • Quick feedback • Regression • Gain control of your code
Unit tests Avoid stupid bugs
Tools Tools of the trade Server Build Server Source Control Build Script Dev Machine Unit Testing Framework Test Runner Isolation Framework Code Coverage
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)
Tools 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 • Visual Studio (MSTest) • TestDrive.Net • R# TestRunner • CodeRushTestRunner • NUnitIt
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!
Write better tests 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 • After refactoring I need to re-write my tests. • Writing the same code twice is wrong
Write better tests 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 Test should be easy to run • Integration vs. Unit tests • Simple configuration • Use fakes
Write better tests Fragile tests • Tests fail every time I change my code
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)
Testing un-testable Testing the un-testable • First - better understand the problem • Isolation framework • Design/refactor for Testability • Reflection • Use integration tests
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