24
Unit Testing in .net

Unit testing basics with NUnit and Visual Studio

Embed Size (px)

DESCRIPTION

Learn unit testing basic step by step.

Citation preview

Page 1: Unit testing basics with NUnit and Visual Studio

Unit Testing in .net

Page 2: Unit testing basics with NUnit and Visual Studio
Page 3: Unit testing basics with NUnit and Visual Studio

Technical reasons

Tests encourage better design sooner Tests are the best documentation Tests are your safety net

Every day useMajor refactoring freedom

Page 4: Unit testing basics with NUnit and Visual Studio

Test Types Unit Testing

Tests an atomic unit of code, such as a method

Integration TestingTests two or more software modules as a group

User Acceptance TestingTests performed by end users to validate specific features

TDD TestLook like a unit test, feels like an user acceptance test

Page 5: Unit testing basics with NUnit and Visual Studio

Unit Testing Defined A unit test is a piece of code that invokes the

method or class being tested and then checks some assumptions about the logical behavior of that method or class. A unit test is almost always written using a unit-testing framework.

The Art of Unit Testing by Roy Osherove

Page 6: Unit testing basics with NUnit and Visual Studio

Unit vs. Integration Testing

Unit tests exercise and test a single piece of code

Integration tests exercise and test many units of code that work together as a whole

Page 7: Unit testing basics with NUnit and Visual Studio

Code to Unit Test

Unit tests should target logical code Logical code is defined as any piece of code

that contains any type of decision making logic such as IF statements, loops, switch statements, calculations etc.

Page 8: Unit testing basics with NUnit and Visual Studio

Properties of a Good Unit Test

It is Automated and Repeatable It is Easy to Implement It Can Be Run in the Future It Can Be Run by Anyone It Can Run at the Push of a Button It Runs Quickly

Page 9: Unit testing basics with NUnit and Visual Studio

Test Project Creation Guidelines

Demo and setup

Page 10: Unit testing basics with NUnit and Visual Studio

Test Project Creation Guidelines

Create separate test projects for unit tests and integration tests. This will allow for calling unit tests from the build process in the future

Test project naming convention:[Project Name].UnitTests[Project Name].IntegrationTests

Page 11: Unit testing basics with NUnit and Visual Studio

Test Class Guidelines

Each class being tested should have its own test class defined (the one-test-class-per-class pattern)

Create utility methods if multiple tests need the same functionality

Test class naming convention:[Class Name]Tests.cs

where Class Name is the name of the class being tested.

Page 12: Unit testing basics with NUnit and Visual Studio

Test Method Guidelines

Each public method exposed by the class under test should be tested// Configure/Setup// Action// AssertNote: you can test Internal methods (if required). Use InternalsVisibleTo[<Project>.UnitTests] attribute in AssemblyInfo.

Test method naming convention:[Method Name]_[Scenario]_[ExpectedBehavior]

Examples: ValidateCreditCard_InvalidExpiryDate_ThrowsException()

ValidateCreditCard_InvalidAddress_ThrowsException()

Page 13: Unit testing basics with NUnit and Visual Studio

NUNIT Test Framework Attributes

Define class as a TestClass

[TestFixture]public class FileManagerTests { }

Define a test method test a single method and expected behavior

[Test]public void IsValidFileName_InvalidFilePath_ThrowException(){ }

Use setup attribute to call required code before each test in the class. For e.g. InitializeConfiguration()

Page 14: Unit testing basics with NUnit and Visual Studio

Test Project Structure

The test project, class, and method guidelines allow us to do the following:Look at a project and find all related testsLook at a class and find all related testsLook at a class method and find all related tests

Page 15: Unit testing basics with NUnit and Visual Studio

Unit Testing Techniques An external dependency is an object in the

system under test over which you have no control.

External dependencies need to be “faked”. It is necessary to fake those dependencies to make sure the scope of the code being tested is limited to the system under test.

Page 16: Unit testing basics with NUnit and Visual Studio

Unit Testing Techniques Stub:

A controllable replacement for an existing external dependency in the system.

It allows to test code without dealing with dependencies directly, since those dependencies are effectively simulated.

can never fail a test

Mock: A fake object in the system that decides whether the unit test passed or

failed. The unit test verifies the state of the mock object to determine success

or failure. may result in failed tests. State of the object would be checked as asserts gets created against them.

Page 17: Unit testing basics with NUnit and Visual Studio

.. And Dependency injection Decouple the components by injecting

them via constructors. Increase the testability.

Page 18: Unit testing basics with NUnit and Visual Studio

Mocks and Stubs – Things to Consider

Writing mocks and stubs takes time, so keep them as simple as possible

Focus on Reusability Inject stub/mocks to replace the dependency

component. Use available libraries e.g. RhinoMocks

Page 19: Unit testing basics with NUnit and Visual Studio

Test Code Coverage

Above 95% code coverage Do not confuse with covering entire assembly . Method level would be sufficient Use tools(Inbuilt or third party).

Page 20: Unit testing basics with NUnit and Visual Studio

Hallmarks of Quality Tests

Trustworthiness – Developers will want to run trustworthy tests, which are tests whose results are reliable

Maintainability – Non-maintainable tests are short-lived. The more focused and short the test methods, the more maintainable.

Readability - If the tests are not easily readable, they will most likely be less trustworthy and less maintainable

Page 21: Unit testing basics with NUnit and Visual Studio

Signs of Trustworthy Tests

When a test passes, your first reaction is not to say “I’ll step through the debugger just to make sure”

When a test fails, your first reaction is not to say, “Well, that does not mean the code is not working”

When you run the tests, your first reaction is not to say “Yes but, what about this scenario?” due to lack of coverage

Page 22: Unit testing basics with NUnit and Visual Studio

Why UnitTests?

Saves your time of debugging via print statements, traces or VS debugger.

Saves other’s time from stepping through your code to ensure it’s working.

Reduce bugs in early stage Better design

Page 23: Unit testing basics with NUnit and Visual Studio

Resources

Nunithttp://www.nunit.org

Code Coverage Overviewh

ttp://msdn.microsoft.com/en-us/library/cc667391(v=VS.90).aspx

Rhino Mockshttp://ayende.com/blog/tags/rhino-mocks

Page 24: Unit testing basics with NUnit and Visual Studio

Blog – www.cshandler.com

About me

Amit ChoudharyMicrosoft MVP Visual C#

Twitter – @vendettamit