Files
freeCodeCamp/guide/english/csharp/unit-testing/index.md
Randell Dawson 0a1eeea424 fix(guide) Replace invalid prism code block names (#35961)
* fix: replace sh with shell

fix replace terminal with shell

fix replace node with js

fix replace output with shell

fix replace cs with csharp

fix replace c++ with cpp

fix replace c# with csharp

fix replace javasctipt with js

fix replace syntax  with js

fix replace unix with shell

fix replace linux with shell

fix replace java 8 with java

fix replace swift4 with swift

fix replace react.js with jsx

fix replace javascriot with js

fix replace javacsript with js

fix replace c++ -  with cpp

fix: corrected various typos

fix: replace Algorithm with nothing

fix: replace xaml with xml

fix: replace solidity with nothing

fix: replace c++ with cpp

fix: replace txt with shell

fix: replace code with json and css

fix: replace console with shell
2019-05-15 19:08:19 +02:00

52 lines
2.3 KiB
Markdown

---
title: Unit Testing
---
# Unit Testing
Unit Testing is a way of checking that a small chunk of your code, or a 'unit', works as you expect it to and it is very good practice for producing high quality code.
It is particularly useful in large projects, to quickly and easily track down what unit is causing defects in your wider project.
In commercial development the majority of bugs and issues with code can be mitigated through the use of unit testing. Unit testing also allows for automation testing.
Unit testing is also closely used in conjunction with test driven development.
## Creating a basic unit test
1. Right click ```Solution``` > ```Add``` > ```New Project```
2. Select ```Unit Test App (Universal Windows)``` and name the UnitTest project appropriately
3. Reference the solution you are testing, by right clicking the ```References``` tab in the unit test > ```Add Reference``` and select the solution that you are referencing. Then make the class that you are testing publicly accessible.
4. Name the TestMethods appropriately, and then input the ```//Arrange``` ```//Act``` ```//Assert``` parameters within the method body code block.
5. Under the ```// Arrange``` section, each variable involved in that individual test needs to be declared, as well as the ```expectedResult```.
6. Under the ```// Act``` section, the variables that are going to be passed as an input into a given method are placed into the method's parenthesis, and all of this is initialised as an ```actualResult``` variable.
7. Under the ```// Assert``` section, to check whether the ```expectedResult``` is equal to the ```actualResult``` and then assert this finding to make the test pass using the ```Assert.AreEqual(expectedResult, actualResult)``` class and method.
## Example code
The code below is testing whether the ```MultiplyPointsMethod``` in the ```Multiply Points Class``` will output ```600``` from an input of 6.
```csharp
[TestMethod]
public void BonusPointsOutputTestWithInt6()
{
// Arrange
var userInput = 6;
var expectedResult = 600;
// Act
int actualResult = MultiplyPointsClass.MultiplyPointsMethod(userInput);
// Assert
Assert.AreEqual(expectedResult, actualResult);
}
```
## Running the test
1. Go to ```Test``` > ```Run``` > ```All Tests```
2. If the testing window does not pop up go to ```Test``` > ```Windows``` > ```Test Explorer```