简体   繁体   English

我需要一个解决方案来创建 xUnit 测试吗?

[英]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.我在 .NET 6 中有一个项目,其中包含一个我想要测试的一类简单控制台应用程序。 I wish to create a second class with xUnit test, but they do not run.我希望用 xUnit 测试创建第二个 class,但它们没有运行。

do.net test prints only do.net 仅测试打印

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测试.cs

using Xunit;

namespace b;
public class Tests
{
    [Fact]
    public void One()
    {
        Assert.True(Parser.Check("{}{}{}"));
    }
}

Program.cs程序.cs

using b;

do
{
    string line = Console.ReadLine();
    Console.WriteLine(Parser.Check(line));
}while(true);

Parser.cs解析器.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?有更简单的方法吗?

By the looks of it, you do not have just one class, but two: Program.cs and Parser.cs从外观上看,您不仅有一个 class,还有两个: Program.csParser.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.我建议创建另一个测试项目(尤其是使用 IDE 之类的 Visual Studio 来帮助您生成测试项目)并在其中添加测试 class 或类。

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.由于您必须将测试与成品一起发送,因此其依赖项(xUnit 运行器、VS 测试 SDK 等)也将自动包含在您的部署中。
  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).这是因为您正在构建可执行文件,而 xUnit 期望测试位于 class 库 (DLL) 中。 Furthermore, the class library that contains the tests must target an executable framework (not a portable one like .NET Standard).此外,包含测试的 class 库必须针对可执行框架(而不是像 .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.所以是的,您应该创建一个包含 2 个独立项目的解决方案。

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.我什至建议你更进一步,在你的解决方案的根目录下添加一个顶级/src/test目录,这样以后如果你有更多的程序集,就有一个合理的地方来放置它们并为每个程序集共享通用配置环境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我需要多个断言吗? xUnit测试 - Do I need multiple assert? xUnit Test xunit测试是否完全隔离运行? - Do xunit tests run in total isolation? 我需要一个解决方案来进行类型参数重载 - i need a solution to do type parameter overloading 如何使用MSTest / xUnit框架编写上下文/规范样式单元测试? - How do I write context/specification style unit tests with an MSTest/xUnit framework? 如何针对net45的类库(包)项目运行xunit测试? - How do I run xunit tests for a Class Library (Package) project targeting net45? 尝试创建 xUnit 测试时出现 CS0246 错误 - CS0246 error while trying to create xUnit tests 当我在夹具类中创建新的自定义 FluentValidator 对象时,.Net Core 3.1 Xunit runnig 测试会导致堆栈溢出错误 - .Net Core 3.1 Xunit runnig tests causes stack overflow error when I create new Custom FluentValidator object in fixture class 我想为此控制器创建 Xunit 测试。 我怎样才能做到这一点 - I want to Create Xunit test for this controller. How can i do that 如何在 Visual Studio 2019 中创建 .NET framework 4.6 版本的 XUnit 项目? - How do I create .NET framework 4.6 version of XUnit project in Visual Studio 2019? 如何在 Xunit 中创建一个基础测试 class 来测试接口实现? - How do I create a base test class in Xunit to test interface implementations?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM