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

Core

This document provides an introduction and overview of unit testing in ASP.NET Core applications. It discusses what unit testing is, the benefits of finding bugs early in the development process, and different levels of testing including unit, integration, and system testing. It also provides details on setting up unit testing in Visual Studio, including creating a unit test project, adding references to the code under test, and writing example unit test code to validate a Celsius to Fahrenheit conversion method.

Uploaded by

AC Mane
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Core

This document provides an introduction and overview of unit testing in ASP.NET Core applications. It discusses what unit testing is, the benefits of finding bugs early in the development process, and different levels of testing including unit, integration, and system testing. It also provides details on setting up unit testing in Visual Studio, including creating a unit test project, adding references to the code under test, and writing example unit test code to validate a Celsius to Fahrenheit conversion method.

Uploaded by

AC Mane
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

ASP.

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

Code (SDLC) Specifications


Gantt Chart
System Documentation
Design SDD Software Design Documents
with ER Diagram, UML Diagrams, CAD Drawings
Why Find Bugs early?

Software Development Life Cycle (SDLC)


Testing

Validation Testing Defect Testing


Demonstrate to the Developer and the Find inputs or input sequences where
Customer that the Software meets its the behavior of the software is incorrect,
Requirements. undesirable, or does not conform to its
specifications.
Custom Software: These are caused by defects (bugs) in
There should be at least one test for every the software.
requirement in the SRS document.
Generic Software:
There should be tests for all of the system
features that will be included in the product
release.
I. Sommerville, Software Engineering, 10 ed.: Pearson, 2015.
Test Categories
Black-box vs. White-box Testing
Black-box Testing: You need no
knowledge of how the system is created.

White-box Testing: You need to have


knowledge of how (Design and
Implementation) the system is built Typically done by Developers, etc
Levels of Testing
Is the responsibility of the customer – in general. The goal
is to gain confidence in the system; especially in its non-
Acceptance Testing functional characteristics

System Testing The behavior of the whole product (system) as


defined by the scope of the project

Integration Testing Interface between components; interactions


with other systems (OS, HW, etc)

Unit Testing
Any module, program, object separately
testable
Levels of Testing
Unit Testing: Test each parts
independently and isolated

Regression Testing: Test


Integration Testing: Make sure that it still works after a
that different pieces work change in the code, i.e.,
together. Test the Interfaces run all Unit Tests, etc.
between the different pieces.
Interaction with other systems
(Hardware, OS, etc.)
System Testing: Test the whole system
Levels of Testing
Requirements & Design
Start
Start Development
Unit Tests are written by the Developers as part of the
Unit Testing Programming. Each part is developed, and Unit tested
separately (Every Class and Method in the code)
Regression testing is testing the system to check that
Regression Testing changes have not “broken” previously working code.
Both Manually & Automatically (Re-run Unit Tests)
Integration testing means the system is put together
Integration Testing
and tested to make sure everything works together.

System/Validation System testing is typically Black-box Tests that validate


Testing the entire system against its requirements, i.e Checking
that a software system meets the specifications
The Customer needs to test and approve the software
Acceptance Testing
Finish before he can take it into use. FAT/SAT.
Unit Testing

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

We will use MSTest Test Project (.NET Core)


Basic Concept in Unit Testing
The basic concept in Unit Testing is to Compare the
results when running the Methods with some Input
Data (“Actual”) with some Known Results (“Expected”)
The Assert Class contains different Methods that can
Example: be used in Unit Testing
...

Assert.AreEqual(expected, actual, 0.001, ”Test failed because...");

All Unit Tests


Framework have the Error margin Error message shown if
Assert Class Compare
the Test fails
Unit Tests – Best Practice
• A Unit Test must only do one thing
• Unit Test must run independently
• Unit Tests must not be depending on the environment
• Test Functionality rather than implementation
• Test public behavior; private behavior relates to implementation
details
• Avoid testing UI components
• Unit Tests must be easy to read and understand
• Create rules that make sure you need to run Unit Tests (and they
need to pass) before you can Check-in your Code in the Source
Code Control System
http://www.uio.no/studier/emner/matnat/ifi/INF5530
Unit Testing in Visual Studio

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)

A simple sketch of the User Interface:

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

We get wrong Answer!


Unit Test Project
Create Unit Test Project
Create Unit Test Project
Create Unit Test Project
You have now 2 Projects in your Solution Explorer
Add Reference to the Code under Test
Create the Unit Test Code
Create the Unit Test Code
using Microsoft.VisualStudio.TestTools.UnitTesting;
using FahrenheitApp.Models;

namespace UnitTestTemperature
{
[TestClass]
public class UnitTestFahrenheit
{
[TestMethod]
public void TestFahrenheitConversion()
{
double temperatureCelcius = 22;
double temperatureFahrenheitActual;
double temperatureFahrenheitExpected = 71.6;

temperatureFahrenheitActual = Temperature.CelciusToFahrenheit(temperatureCelcius);

Assert.AreEqual(temperatureFahrenheitExpected, temperatureFahrenheitActual, 0.001, "Temperature conversion


not correctly");
}
}
}
Test Explorer
Start Running the Unit Test
Test Results
9
Debugging 𝑇𝐹 = 𝑇𝐶 + 32
namespace FahrenheitApp.Models
5
{
public static class Temperature
{
public static double CelciusToFahrenheit(double Tc)
{
double Tf;

Tf = 9 / 5 * Tc + 32; Probably Error in Formula?


What is wrong?
return Tf;
}
}
}
9
Fixing Bugs 𝑇𝐹 = 𝑇𝐶 + 32
namespace FahrenheitApp.Models
5
{
public static class Temperature
{
public static double CelciusToFahrenheit(double Tc)
{
double Tf;

Tf = Tc * 9/5 + 32;

return Tf;
}
}
}
Re-run Unit Test

Everything Works! The Test Passed!


Checking Code Coverage
Note! The code coverage feature is available only in Visual Studio Enterprise edition.
Code Coverage
• Code coverage is a measure used in software testing. It describes
the degree to which the source code of a program has been
tested.
• Depending on the input arguments, different parts of the code will
be executed. Unit Tests should be written to cover all parts of the
code.
Note! The code coverage
feature is available only
in Visual Studio
Enterprise edition.
Code Coverage Results

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

You might also like