2. Agenda
Importance of Unit Testing
Test Driven Development
TDD Techniques
Unit test in .net (VS2010)
1.
2.
3.
MSTest
nUnit
Mock
Big Projects (VS2010 and Eclipse)
3. 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.
Acceptance Testing
System Testing
Integration Testing
Unit Testing
4. 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”
5. 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.
6. Metric of usage TTD in Projects
Level 0 – huh?
Level 1 – we write unit tests.
We write a unit test, then some code, then some more code.
Level 3 – we do Test-Driven Development
We wrote some tests for critical parts.
Level 2 – we write (most of) our unit tests first.
What’s a unit test?
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.
7. TTD = RGR + CT
Refactor
Write a little
test
Cycle loop
< 10min
Get the test
pass
Red-Green-Refactor
Stub the
code.
Watch if
fails
Continuous (integration) Testing
Test Driven Development
8. 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
9. 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
11. TDD Techniques
Common Unit Test Patterns
– 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
Simple-test
12. 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.
13. Unit test in .net
NUnit
MSTest
TestDriven
Moq
RhinoMocks
Typemock
Microsoft Fakes
…
14. 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]
20. New features - better testing
Derived from Microsoft Research “Moles” project
Fakes come in two flavors
– concrete implementations of interfaces or
abstract classes that you can pass in to your systemunder-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!
Stubs
This is the “New Project” dialog that is accessed from the IDE “File” menu. Here is where you add the Test Project to your application’s solution. To add the Test Project to your solution, you must select the “Test Project” template from the template list in the center of the dialog. Then you must give the project a name. The name of the application being tested with “Test” appended to it is a good naming convention to follow for your unit test projects. Choose a location at the same directory level or below your application directory. Be sure you choose “Add to Solution” on the solution line from the dropdown list.
This is an example unit test project for an executable that is part of an ETL process. It contains test class files for each of the test classes in the application. It contains a folder, TestFiles, which contains data files for each of the test classes. Finally, it contains a utility class with common methods used by all of the unit test classes.You may or may not choose to follow this structure. It is not required or necessarily suited to your environment.
Add the Microsoft.VisualStudio.TestTools.UnitTesting namespace with a using directive at the top of your unit test files.
A unit test class must be marked with the [TestClass] attribute in order for tests within the class to be executed by the testing framework.
Each unit test class must have a TestContext property that can be accessed by the test framework.
Example of UnitTest class TestContext declaration section.
Initialize methods are typically used to setup the test environment to a known state. ClassInitialize does this once before any of the test methods are run in the UnitTest class.
ClassInitialize method. This example shows file directories used by tests being cleared, test files being copied, a test database being created and a test table being created. These are the types of tasks done in initialize methods. They bring the test environment into a known state so that the test results can be relied upon to be accurate.
Cleanup methods are the counterparts to initialize methods. They handle any of the tasks that would need to be performed subsequent to test methods executing. The ClassCleanUp method executes after all of the test methods in the unit test class have run.
ClassCleanUp method. This example shows test database being dropped after all tests in class have been executed.
TestInitialize is run prior to each test method in the unit test class executing. This differs from the ClassInitialize in that it performs tasks that must be done before each of the tests is run rather than once before all of the tests are run.
TestCleanUp is the counterpart to TestInitialize. This method will execute after each of the test methods in a unit test class.
Example of tests that test public properties. Test names are the Property Name with the word Test appended to them.
Examples of tests that test a specific requirement. The name of each test clearly indicates what the requirement is. Each method name is appended with the word Test.
For more information about theMicrosoft.VisualStudio.TestTools.UnitTesting namespace see MSDN documentation found at:http://msdn.microsoft.com/en-us/library/ms244252http://msdn.microsoft.com/en-us/library/ms244252
The add new test dialog. Choose the Unit Test item to add a standard unit test to your project.
This is what a default unit test class skeleton looks like before you modify it with your code. It contains a class definition with the [TestClass] attribute. It also contains a definition for the TestContext class variable, commented initialize/cleanup methods and a sample test method skeleton called TestMethod1.
The “Add New Test” dialog with Unit Test Wizard selected.
Unit Test Wizard page that allows you to select what tests you wish to have generated.
Adding tests when you create a new property or method in your code is easy. Place the cursor within the body of the property/method. Right-click the mouse to display the context menu. Choose the “Create Unit Tests” option. Accept the defaults Visual Studio provides. Click OK. The new skeleton method will be added to the class that corresponds to your application class.
Test List Editor with tests grouped by Class Name for easiest viewing/selecting of tests to run. Green arrow on upper left of window is for running or debugging tests. Choose by clicking the little black down arrow to select whether you run or debug the tests that are checked in the test pane of the editor.You may create custom groups of tests to run. By doing this, you can avoid having to check/uncheck specific tests in left pane before executing them.
Test Results window shows summary of test run. Test runs can be selected from dropdown list at top left of screen. Double-clicking on individual row will show detail window for individual test result.
This window shows details of an individual test result. It is most useful when a test fails as it gives detailed error information.
Test Results window will load most recent run by default.Select run name from dropdown at top of test results window to view other than most recent test run.
To learn more about the Visual Studio 2010 testing framework, general unit testing best practices, unit testing concepts and mastering features of the unit testing framework, see the links provided in the following three reference pages.