Search code examples
c#.netxunit

Do I need a solution to create xUnit tests?


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?


Solution

  • There are compelling reasons why you would separate your test project from your production code.

    1. With all of your tests in the same project as your production code, you have to ship your tests with your finished product.
    2. 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.
    3. 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 calling some substandard test code or mocks in production if you are not careful.
    4. "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 that contains the tests 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.