Console (.NET)
Note… instructions are for Microsoft Visual Studio Enterprise 2022 Version 17.4.4. It may be slightly different for different versions of Visual Studio.
Useful tutorial on testing (but we are using a different test project, XUnit). A little outdated, but very informative.
A good read on testing basics: Unit testing fundamentals
NOTE: These instructions may change as we (your teachers) figure out which is the best testing framework for automated testing on Git.
-
Open Visual Studio
-
Choose
File
->Open
->Project/Solution
, and open your solution -
Choose
File
->New
->Project
-
You must find the
xUnit Test Project (.NET Core)
. Make sure to choose the C# version. -
Click
Next
-
BEFORE CLICKING
CREATE
…Make sure that the “option” for the “solution” isadd to solution
, not “create new solution”.
-
Link your project to the test suite
-
Give your test code access to the code you want to test
-
Open
UnitTest1.cs
NOTE: if
using Xunit
is giving an error, just be patient, Visual Studio will download the necessary files automatically. -
You must
use
the namespace of the code you want to test. -
Example:
using Budget;
-
In your test file, you must include the [FACT]
keyword to indicate which classes/methods are part of testing, as opposed to helper functions.
[Fact]
public void SomethingMethod_Test()
{
// this is a test
}
Tests are validated through a series of Assert
statements, which will cause specific exceptions to be created. The testing framework will capture these exceptions, creating a list of passed and failed tests.
[Fact]
public void CategoriesMethod_Delete()
{
// Arrange
Categories categories = new Categories(testInputFile);
int IdToDelete = 3;
// Act
categories.Delete(IdToDelete);
List<Category> categoriesList = categories.List();
// Assert
Assert.Equal(numberOfCategoriesInFile - 1, sizeOfList);
Assert.False(categoriesList.Exists(e => e.Id == IdToDelete),
"correct Category item deleted");
}
- If any test files already exist, add them to your project the same way you would add files to any project
- a Console .NET project (call it 'Budget' to keep it simple).
- Add all the necessary files (get them from LEA)
- Create a new
Xunit
project (within the same solution)- Don't forget to add the reference to the budget project
- Add all the test files to the test project.
NOTE: For HomeBudget tests, there are additional test files which must also be included into the project. In other words, include all files.
- Delete
unitTest1.cs
-
Select
View
->Test Explorer
- You will now have a panel, where you can choose to run your tests, run specific tests, etc.
-
You may need to rebuild your solution to see the tests.
- From Test Explorer, you can click on the green arrow to choose which tests to run.
-
The details of a failing test (if selected) will be shown in the
Test Detail Summary
window. This information includes a stack trace, which will indicate whichassert
failed.- This will allow you to go directly to the failing Assert.
-
Debugging
- You can set breakpoints in tests, and in your project code, as per usual
- To debug, select test(s) that you wish to debug, right click, and select
debug
In our course, we will eventually be building a WPF (gui) app using the budget code files, however…
-
Out of 60-some tests, there are some failing tests!
- you will expected to debug, and fix them before proceeding to make any modifications
-
In class exercise... debug the failing test:
TestCategories.Categories_TypeAllDayEventReadCorrectlyFromFile
-
what other tests are still failing?