Date post: | 04-Dec-2014 |
Category: |
Education |
Upload: | andrzej-piotrowski |
View: | 472 times |
Download: | 0 times |
TEST DRIVEN DEVELOPMENT +UNIT TESTING
Using Visual Studio 2010/2012 and EclipseBy Andrzej Piotrowski
Agenda
Importance of Unit Testing Test Driven Development TDD Techniques Unit test in .net (VS2010)
1. MSTest2. nUnit3. Mock
Big Projects (VS2010 and Eclipse)
Unit Testing
A unit is the smallest testable part of software. In object-oriented programming, the smallest unit
is a method, which may belong to a base/super class, abstract class or derived/child class.
Unit Testing
Integration Testing
System Testing
Acceptance Testing
Unit testing myths
“Testers test, developers develop” “There’s no time to write tests.” “We’ll write the unit tests later.” “The code will change, so the tests
will fail in the (near) future.” “The test we wrote some time ago
(probably) don’t work anymore.” “Not easy to maitain for big projects” “Time waste for existing projects”
Test Driven Development (TDD)
A software development technique that uses short development iterations based on pre-written test cases that define desired improvements or new functions.
Each iteration produces code necessary to pass that iteration's tests. Finally, the programmer or team refactors the code to accommodate changes.
A key TDD concept is that preparing tests before coding facilitates rapid feedback changes.
Metric of usage TTD in Projects
Level 0 – huh? What’s a unit test?
Level 1 – we write unit tests. We wrote some tests for critical parts.
Level 2 – we write (most of) our unit tests first. We write a unit test, then some code, then some more
code. Level 3 – we do Test-Driven Development
We write a unit test, then we write some code to make it pass. Then we refactor, and do it all over again.
Level 4 – we do Acceptance Test-Driven development We automate our acceptance criteria into acceptance
tests first, and then use failing AT’s until our AT’s pass.
TTD = RGR + CT
Write a little test
Stub the code.
Watch if fails
Get the test pass
Refactor
Cycle loop < 10min
Continuous (integration) TestingRed-Green-Refactor
Test Driven Development
Test Driven Development Rules
Adding New Functionality “Test First” ( Red) (Green) Write an “empty” implementation Write a failing test: this is your requirements Make the test pass Requirements are completed
Fixing a Bug (Red) Write a failing test that demonstrates the bug Fix the bug The test will make sure the bug does not happen again
Changing Implementation (Refactor) Make your changes Make sure all tests pass All requirements/change request are satisfied
TTD – words of wisdom
Arrange Setup. Set up all the data and conditions that
the test needs to execute. Create anything that is needed for the test to operate independently of its environment.
Mock out any dependent systems Control all variables not under test
Act Execution – execute the mainline code
Assert Validate that the code operated as intended
Visual Studio 2010
TDD Techniques
Common Unit Test Patterns Simple-test – provide input and check result
for acceptance or failure. Code-path – provide values that force the
code through a specific path. Parameter-range – pass a set of parameters
that cause success, and a set that cause failure.
Data-driven – pass in sets of data and their expected outcomes
Dependency Injection
More TDD Techniques
Mock Objects Mocks are used to remove dependencies on
external systems or components. Example: Mock Database The mock will just be programmed to return
certain packages of data according to which test is being run.
Unit test in .net
NUnit MSTest TestDriven Moq RhinoMocks Typemock Microsoft Fakes …
nUnit
Important Attributes
1. [SetUp]SetUp is generally used for any initialization purpose. Any code that must be initialized or set prior to executing a test, are put in functions marked with this attribute. As a consequence, it avoids the problem of code repetition in each test.
2. [Ignore]
3. [ExpectedException]
4. [TearDown]
MSTest
the [TestMethod()] attribute. Without the TestClass attribute, the test methods are ignored
nUnit vs MSTest
Moq
Architecture in Visual Studio 2012
Visual Studio Unit Test Explorer
Command Line Runner
TeamBuild Unit Test Activity
Visual Studio Unit Test Platform
MS-Test Manage
d
MS-Test Native
NUnitxUnit.ne
tQUnit MORE!
Developer Focused Unit Test Experience
Red-Green Bar
Most important
tests shown first
Timings
Shows tests from any
framework
Search
Run Details
New features - better testing
Derived from Microsoft Research “Moles” project
Fakes come in two flavors Stubs – concrete implementations of interfaces or
abstract classes that you can pass in to your system-under-test to isolate it from real implementations
Shims – generated classes that enable you to intercept and replace calls to existing classes, even those from the .NET BCL!
References
http://msdn.microsoft.com/en-us/library/dd264975.aspx
http://msdn.microsoft.com/en-us/library/ms182409
http://blog.stevensanderson.com/2009/08/24/writing-great-unit-tests-best-and-worst-practises/
http://en.wikipedia.org/wiki/Unit_testing
References
http://weblogs.asp.net/adilakhter/archive/2008/05/04/more-on-unit-testing-testcontext.aspx
http://www.c-sharpcorner.com/uploadfile/dommym/a-test-driven-development-tutorial-in-C-Sharp-4-0/
http://msdn.microsoft.com/en-us/vstudio/ff718185.aspx
http://www.simple-talk.com/sql/t-sql-programming/close-these-loopholes---reproduce-database-errors/
Eclipse
http://www.happyprog.com/tdgotchi/