Core
Core
NET Core
Unit Testing
Hans-Petter Halvorsen
Contents
1. What is Testing?
– Short Introduction to Testing
2. What is Unit Testing?
3. Unit Testing in Visual Studio
Introduction to Testing
Hans-Petter Halvorsen
End-User User Guides Installation
Documentation Guides Planning
System
Deployment
SDP
Documentation Maintenance Software Development
Plan
Project Planning
Testing
STD The Software
Software Test Documentation
Gantt Chart
Software Test Plan (STP)
Test
Documentation
Development Requirements
Analysis
Implementation Lifecycle SRS
Software Requirements
Unit Testing
Any module, program, object separately
testable
Levels of Testing
Unit Testing: Test each parts
independently and isolated
Hans-Petter Halvorsen
Unit Testing
System to be Tested Then we take out each Unit
and Test it by making a Unit
Test for each piece of your
system
What are Unit Tests
• Unit Testing (or component testing) refers to tests
that verify the functionality of a specific section of
code, usually at the function level.
• In an object-oriented environment, this is usually at
the class and methods level.
• Unit Tests are typically written by the developers as
part of the programming
• Automatically executed (e.g., Visual Studio and
Team Foundation Server have built-in functionality for Unit Testing)
Test Driven Development (TDD)
• Coding and Testing are done in parallel
• The Tests are normally written before the Code
• Introduced as part of eXreme Programming
(XP) (an Agile method)
• Unit Tests are important part of Software
Development today – either you are using TDD
or not
Unit Tests Frameworks in Visual Studio
• MSTest
• NUnit
• xUnit
Hans-Petter Halvorsen
Unit Testing in Visual Studio
• Visual Studio have built-in features for Unit
Testing
• We need to include a “Test Project” in our
Solution
Test Method Requirements
A test method must meet the following
requirements:
• The method must be decorated with the
[TestMethod] attribute.
• The method must return void.
• The method cannot have parameters.
Example
Unit Testing in Visual Studio
Hans-Petter Halvorsen
ASP.NET Core Application
Convert to Fahrenheit
Create the following Application (e.g., WinForm App or ASP.NET App)
Celsius: Fahrenheit:
22 ℃ Convert 71,6 ℉
Conversion Formula:
9
𝑇𝐹 = 𝑇𝐶 + 32
5
User Interface
Add Class i your Models Folder
namespace FahrenheitApp.Models
{
public static class Temperature
{
public static double CelciusToFahrenheit(double Tc)
{
double Tf;
Tf = 9 / 5 * Tc + 32;
return Tf;
}
}
}
Create your GUI
Testing
9
𝑇𝐹 = 𝑇𝐶 + 32
5
9
𝑇𝐹 = ∙ 22 + 32
5
=71.6
namespace UnitTestTemperature
{
[TestClass]
public class UnitTestFahrenheit
{
[TestMethod]
public void TestFahrenheitConversion()
{
double temperatureCelcius = 22;
double temperatureFahrenheitActual;
double temperatureFahrenheitExpected = 71.6;
temperatureFahrenheitActual = Temperature.CelciusToFahrenheit(temperatureCelcius);
Tf = Tc * 9/5 + 32;
return Tf;
}
}
}
Re-run Unit Test
In this case the Unit Test covered 100% of the code. If we use If…Else… or similiar, we typically
need to write Unit Test for each If…Else… in order to cover all the Code
References
• https://docs.microsoft.com/en-
us/visualstudio/test/getting-started-with-unit-
testing
Hans-Petter Halvorsen
University of South-Eastern Norway
www.usn.no
E-mail: [email protected]
Web: https://www.halvorsen.blog
YouTube: https://www.youtube.com/IndustrialITandAutomation