NUnit 测试 - 循环 - C# [英] NUnit Test - Looping - C#

查看:48
本文介绍了NUnit 测试 - 循环 - C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 TDD 的新手,正在尝试解决问题.

I am new to TDD and trying to solve a problem.

在我的任务中,我必须从控制台读取一堆字符串并将它们添加到字符串类型列表中.在我的测试方法中,我编写了一个 for 循环来读取字符串并传递给要添加的方法.我不知道如何测试这个过程,有点困惑.任何帮助将不胜感激.谢谢.

In my task, I have to read a bunch of strings from console and add them to a list of string type. In my test method, I have written a for loop to read strings and passing to a method to add. I don't know how to test this process, a bit confused. Any help will be appreciated. Thanks.

循环测试方法.

   for(int i=0;i<robot.noOfCommands;i++)
        {
            robot.readCommand(Console.ReadLine());

        } 

我正在用 C#.Net 编写代码

I am writing code in C#.Net

推荐答案

单元测试不应该需要人工交互,所以使用 Console.ReadLine() 是一个主要的禁忌.

Unit tests should never require human interaction, so using Console.ReadLine() is a major no-no.

您可能想要的是为您的 robot 对象提供一些预定义的输入.然后您可以测试 (Assert),结果是否符合您的预期.这就是单元测试的本质.

What you probably want, is to feed your robot object with some predefined input. Then you can test (Assert), that the outcome is what you expect. That is the essence of unit testing.

这篇关于NUnit 测试 - 循环 - C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆