[英]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.cs
和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.
我建议创建另一个测试项目(尤其是使用 IDE 之类的 Visual Studio 来帮助您生成测试项目)并在其中添加测试 class 或类。
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.
所以是的,您应该创建一个包含 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.