Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit Testing for the Absolute Beginner

Similar presentations


Presentation on theme: "Unit Testing for the Absolute Beginner"— Presentation transcript:

1 Unit Testing for the Absolute Beginner
Carolyn Schroeder, October 16, 2017

2 What is Unit Testing Unit Testing involves testing individual units of software A unit usually has one or a few inputs and usually a single output In object oriented programming, the smallest unit is a method

3 Why do Unit Testing? Unit Testing increases confidence in changing code With test driven development, you create the unit tests before you write the code, so you use the unit tests as both design documentation and functional specifications.

4 How do we set up Unit Tests?
Create unit tests that verify the behavior of the code in response to standard, boundary, and incorrect cases of input data Asserts are made about expected output Unit tests can also indicate the an exceptions is expected

5 Our Example We are going to create a class for simple bank account transactions It should handle crediting and debiting amounts of money

6 What should we test? Credit and Debit methods produce the right balance It is not possible to debit and amount larger than the balance All amounts to be credited and debited are larger than or equal to zero

7 Creating Unit Tests I will be using MSTest in Visual Studio 2017
A Unit Test Class must be public and decorated with the TestClass attribute A Unit Test method must be public and decorated with the TestMethod attribute Methods must have a void return type

8 Unit Test vs Integration Testing
Now we have established the BankAccount class is working right Bank Account balances would held in a data access layer Suppose we wanted to test the interaction between the data access layer and a BankService This would be integration testing

9 Fakes The solution is to fake the data access layer and only test that the service is returning the right value This is done using interfaces I am going to create a data access repository interface I am going to use Fake It Easy to fake the interface

10 Set up The IRepository inteface has just one method GetBalance
The interface is injected into the Bank Service We test that the BankService is returning the right value

11 Resources https://github.com/carolynlschroeder/Unit TestingDemo


Download ppt "Unit Testing for the Absolute Beginner"

Similar presentations


Ads by Google