Reputation: 191
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?
Upvotes: 2
Views: 333
Reputation: 56849
There are compelling reasons why you would separate your test project from your production code.
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.
Upvotes: 2
Reputation: 2186
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.
Upvotes: 2