I have a project in .NET 6 with a one-class simple console app, that I want to test. I wish to create a second class with xUnit test, but they do not run.
dotnet test prints only
Copyright (c) Microsoft Corporation. All rights reserved.
Starting test execution, please wait...
A total of 1 test files matched the specified pattern.
Additionally, path to test adapters can be specified using /TestAdapterPath command. Example /TestAdapterPath:<pathToCustomAdapters>.
Tests.cs
using Xunit;
namespace b;
public class Tests
{
[Fact]
public void One()
{
Assert.True(Parser.Check("{}{}{}"));
}
}
Program.cs
using b;
do
{
string line = Console.ReadLine();
Console.WriteLine(Parser.Check(line));
}while(true);
Parser.cs
namespace b;
public class Parser
{
static Stack<char> s = new Stack<char>();
public Parser()
{ }
static public bool Check(string stringToCheck)
{
int i = 0;
do
{
if(i < stringToCheck.Length - 1)
{
do
{
s.Push(stringToCheck[i ]);
} while (stringToCheck[i] != ')' && stringToCheck[i] != '}' && stringToCheck[i] != ']');
} else
s.Push(stringToCheck[i]);
if (stringToCheck[i] == ')')
{
if (s.Peek() == '(')
s.Pop();
else
return false;
} else if (stringToCheck[i] == '}')
{
if (s.Peek() == '{')
s.Pop();
else
return false;
} else if (stringToCheck[i] == ']')
{
if (s.Peek() == '[')
s.Pop();
else
return false;
}
i ;
} while (i < stringToCheck.Length);
return true;
}
}
Do I need to bulid entire solution and create two separate projects - one for current application, second for tests? Is there a simpler way?
CodePudding user response:
By the looks of it, you do not have just one class, but two: Program.cs
and Parser.cs
Simple and fast solutions are not always the best. I advise creating another test project (especially with an IDE like Visual Studio that helps you generate test projects) and adding the test class or classes there.
CodePudding user response:
There are compelling reasons why you would separate your test project from your production code.
- With all of your tests in the same project as your production code, you have to ship your tests with your finished product.
- Since you have to ship your tests with the finished product, its dependencies (xUnit runner, VS test SDK, etc) will also automatically be included with your deployment.
- At some point, you or someone else maintaining the project might not be able to determine where the production code ends and the test code begins. The result is you may end up running calling some substandard test code or mocks in production if you are not careful.
- "They do not run". This is because you are building an executable and xUnit expects the tests to be in a class library (DLL). Furthermore, the class library must target an executable framework (not a portable one like .NET Standard).
The idea behind separating your tests into another project will help you avoid all of these issues, make your deployment size smaller, and make your project easier to maintain over time. So yes, you should create a solution with 2 separate projects.
I would even recommend that you take it further and add a top level /src
and /test
directory at the root of your solution so later on if you have more assemblies, there is a logical place to put them and to share common configuration for each environment.