0% found this document useful (0 votes)
233 views

Test Driven Development in Visual Studio 2012

The document discusses Test Driven Development (TDD) in Visual Studio 2012. TDD follows the "Red, Green, Refactor" motto - write a failing test (Red), make the test pass (Green), refactor the code (Refactor). It provides steps to create a CalculatorEngine class library and test project, write an initial failing test for the Add method, generate the Calculator class and method stub, implement the method to pass the test, and verify the test turns green.

Uploaded by

erandika1986
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
233 views

Test Driven Development in Visual Studio 2012

The document discusses Test Driven Development (TDD) in Visual Studio 2012. TDD follows the "Red, Green, Refactor" motto - write a failing test (Red), make the test pass (Green), refactor the code (Refactor). It provides steps to create a CalculatorEngine class library and test project, write an initial failing test for the Add method, generate the Calculator class and method stub, implement the method to pass the test, and verify the test turns green.

Uploaded by

erandika1986
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Test Driven Development (TDD) in Visual Studio 2012

In TDD unit tests are written before the code under test. The motto of test –driven Development is “Red,
Green, Refactor”.(According to Microsoft)

 Red: Create a test and make it fail.


 Green: Make the test pass by any means necessary.
 Refactor: Change the code to remove duplication in your project and to improve the design
while ensuring that all test still pass.

So let’s start our practical example.

1. Create a Class library call CalculatorEngine.

Author: Erandika Sandaruwan


Email: [email protected]
2. Delete the class which is in the class library.

Author: Erandika Sandaruwan


Email: [email protected]
3. Create test project and named it as CalculatorEngineTests.

Author: Erandika Sandaruwan


Email: [email protected]
4. Write a Unit test method to test the Add method. So here we create an object from Calculator
class but still Calculator class does not exists. When you click on the arrow Which is under
Calculator and then click on the “Generate new type” and you will see window called” Generate
New Type”.

Author: Erandika Sandaruwan


Email: [email protected]
5. Select the relevant project that you want to implement the class. Here you have to select the
“CalculatorEngine” project and click on OK button. Now Calculator class will create inside the
“CalculatorEngine” project.

Author: Erandika Sandaruwan


Email: [email protected]
6. Now click on the arrow which is under “Add” method and click on Generate method stub. Now
this will generate the “Add” method. But you have to implement the method logic. Now if you
run the Unit Test then it will fail and you will see it in red color.

Author: Erandika Sandaruwan


Email: [email protected]
Author: Erandika Sandaruwan
Email: [email protected]
7. Now we can implement the method logic inside the method. Here we have to implement only
one line of code. “ return p + p_2 ; ”

Author: Erandika Sandaruwan


Email: [email protected]
8. Now you can run the Unit Test.

Author: Erandika Sandaruwan


Email: [email protected]
9. If every things work correct you will get the nice green color test result like below.

Author: Erandika Sandaruwan


Email: [email protected]

You might also like