如何在c#visual Studio 2019中的单元测试方法内部使用控制台或终端来显示输出并接收用户的输入?

我正在尝试编写代码,以提示用户并询问他们要测试的测试文件(我需要能够测试传入的不同客户文件,并且所有文件均以客户名开头)。然后,我想将与用户提供的文件匹配的文件移动到文件夹,我创建的单元测试可以在其中捕获它们并对其进行运行测试。即使调用Console.writeLine()函数,我也无法显示控制台(显然,然后我无法在控制台中输入任何内容以响应应在控制台上显示的内容)。 / p>

我尝试使用[ClassInitialize]标记并调用在该标记的方法内提示用户的方法。我也尝试在单元测试方法([TestMethod])的顶部调用相同的方法,但这些方法也不起作用。我知道测试浏览器中的输出将在“为此结果打开其他输出”下,但这对我没有帮助。

    [TestMethod]
    public void CompareModels()
    {
        Console.WriteLine("Please enter customer that you want to test: ");
        var userInput = Console.ReadLine();
        var testFiles = Directory.GetFiles("my test file directory");
        foreach(var file in testFiles)
        {
            if(file.Contain(userInput)
                File.Move(file,"my in testing directory");
        }
    }

我希望控制台出现在代码的第一行,但不会出现,并且程序在控制台上永远等待。ReadLine()因为我无法在不会出现的控制台中输入任何输入。 我还要补充一点,如果用户几秒钟不输入任何内容,那么测试将使用“正在测试”目录中的文件运行。因此,用户可以更改被测试的客户而无需手动移动文件,或者仅运行测试即可为他们完成所有工作。

liu0659222 回答:如何在c#visual Studio 2019中的单元测试方法内部使用控制台或终端来显示输出并接收用户的输入?

正确的方法是将要测试的逻辑与UI隔离,并在可能的情况下与物理数据源(文件系统,数据库)隔离。如果我们将纯逻辑注入文件服务的抽象,则可以对其进行测试。对于测试,我们可以提供一个测试假人。您可以使用正确的参数测试虚拟方法是否被调用了预期次数。当然,如果您愿意为此设置测试文件夹和文件,则可以提供真实的文件服务。

public interface IFileService
{
    string[] GetFiles(string path);
    void Move(string sourceFileName,string destFileName);
}

通过注入服务实现逻辑:

public class FileMover
{
    private readonly IFileService _fileService;

    public FileMover(IFileService fileService)
    {
        _fileService = fileService;
    }

    public void MoveFiles(string sourceDir,string destinationDir,string filterText)
    {
        string[] testFiles = _fileService.GetFiles(sourceDir);
        foreach (string file in testFiles) {
            if (file.Contains(filterText)) {
                _fileService.Move(file,destinationDir);
            }
        }
    }
}

您可以使用模拟框架来创建测试假人。他们从您可以配置为执行操作的界面中自动创建一个检测类。虚拟对象会自动计算方法调用并注册其参数,返回您配置的内容等。

在没有控制台的情况下进行测试:

[TestMethod]
public void CompareModels()
{
    // Arrange
    IFileService fileService = Substitute.For<IFileService>();
    //TODO: setup fileService Dummy.

    var sut = new FileMover(fileService); // sut stands for Service Under Test.
    string sourceDir = @"C:\Test\Source";
    string destDir = @"C:\Test\Destination";
    string userInput = "abcd";

    // Act
    sut.MoveFiles(sourceDir,destDir,userInput);

    // Assert
    //TODO: test whether the fileService methods have been called as expected.
}

在真实的控制台应用中:

var fileService = new RealFileService();
var mover = new FileMover(fileService);

Console.WriteLine("Please enter customer that you want to test: ");
string userInput = Console.ReadLine();
mover.MoveFiles("my test file directory","my in testing directory",userInput);

因此,如果您这样做,则需要使用Dependency injection容器。这要求您通过界面定义所有服务。

public interface IFileMover
{
    void MoveFiles(string sourceDir,string filterText);
}

public class FileMover : IFileMover
{
    ...
}

设置DI容器:

var diContainer = new SomeDependencyInjectionContainer();
diContainer.Register<IFileService,RealFileService>();
diContainer.Register<IFileMover,FileMover>();

DI容器自动识别并创建必须通过构造函数注入的服务。因此,您只需使用以下命令即可创建文件移动器

var mover = diContainer.Resolve<IFileMover>();

将自动注入RealFileService

本文链接:https://www.f2er.com/3143541.html

大家都在问