简体   繁体   English

C# NUnit 测试

[英]C# NUnit testing

I have just started to learn C# and want to start testing using NUnit.我刚刚开始学习 C#,想开始使用 NUnit 进行测试。 I thought I would start very simple and test a "Hello World" program, just to get me used to NUnit in general.我想我会从非常简单的开始并测试一个“Hello World”程序,只是为了让我习惯于一般的 NUnit。

Here is my code:这是我的代码:

HelloWorld.cs你好世界

using System;

namespace TDDHelloWorld
{
    public class HelloWorld
    {
        static void Main()
        {
        }

        public static string HelloMessage()
        {
            return "Hello World";
        }
    }
}

HelloWorldTest.cs HelloWorldTest.cs

using Microsoft.VisualStudio.TestPlatform.TestHost;
using NUnit.Framework;
using TDDHelloWorld;

namespace HelloWorldTests 
{
    [TestFixture]
    public class HelloWorldTest
    {
        [Test]
        public void SayHelloWorld()
        {
            string expected = "Hello World";

            Assert.AreEqual(expected, HelloWorld.HelloMessage());
        }
    }
}

I have a few questions:我有几个问题:

  1. What should be in my static void Main {} ?我的static void Main {}应该有什么?
  2. The following error occurs:出现以下错误:

Can't get source code location for : HelloWorldTests" -无法获取源代码位置:HelloWorldTests”-

what does this mean?这是什么意思?

  1. Is this even correct?这甚至正确吗? I am so new to C# I don't feel confident.我对 C# 很陌生,我没有信心。

If it's relevant, I'm using Visual Studio Community on a MacBook.如果相关,我将在 MacBook 上使用 Visual Studio Community。 I've tried researching testing but struggling to grasp even the simplest test.我尝试过研究测试,但即使是最简单的测试也很难掌握。

If anyone can help it would be really appreciated!如果有人可以提供帮助,将不胜感激! :) :)

One of the most challenging aspects of Unit Testing is knowing what to test and what not to test.单元测试最具挑战性的方面之一是知道什么该测试,什么不该测试。

This challenge is subtle -- there is at least one entire book (probably more) devoted to this subject: The Art of Unit Testing by Roy Osherove.这个挑战很微妙——至少有一整本书(可能更多)专门讨论这个主题:Roy Osherove的《单元测试的艺术》

In your case you are learning about C# and Unit Testing at the same time, using a very simple console application as the 'system under test'.在您的情况下,您同时学习 C# 和单元测试,使用一个非常简单的控制台应用程序作为“被测系统”。 Keeping it simple is a great approach when learning;保持简单是学习时的好方法; but you should realize that particularly in the case of Unit Testing concepts you will struggle trying to 'project' the ideas into practical-real-world instances.但是您应该意识到,尤其是在单元测试概念的情况下,您将很难尝试将这些想法“投射”到实际的实际实例中。

Don't let this discourage your experimentation and learning, because you can definitely build upon what you are learning.不要让这阻碍您的实验和学习,因为您绝对可以建立在您正在学习的基础上。

You asked:你问:

What should be in my static void Main {} ?我的static void Main {}应该有什么?

This method is the entry point for your 'application'.此方法是您的“应用程序”的入口点。 It should be calling your HelloMessage() method (because that is the only code you have) and I'm guessing you want classic 'Hello World' behavior so you should be outputting the string returned by HelloMessage() :它应该调用你的HelloMessage()方法(因为这是你唯一的代码),我猜你想要经典的“Hello World”行为,所以你应该输出HelloMessage()返回的HelloMessage()

Console.WriteLine( HelloMessage() );

But this has nothing at all to do with NUnit or Unit Testing.但这与 NUnit 或单元测试毫无关系。 This is just convention that is part of C# console applications.这只是 C# 控制台应用程序一部分的约定。 If your entry point does not have any code then your application will not do anything.如果您的入口点没有任何代码,那么您的应用程序将不会执行任何操作。

The following error occurs: Can't get source code location for : HelloWorldTests - what does this mean?出现以下错误: Can't get source code location for : HelloWorldTests - 这是什么意思?

Are you compiling in DEBUG or RELEASE?你是在 DEBUG 还是 RELEASE 中编译? A RELEASE compilation does not have debugging symbols available within it and could be the cause of this problem. RELEASE 编译中没有可用的调试符号,这可能是导致此问题的原因。 Also you might be able to workaround this problem by choosing Run Unit Tests rather than Debug Unit Tests .可以通过选择Run Unit Tests而不是Debug Unit Tests来解决此问题。 Give it a try.试一试。

This may be a Visual Studio bug.这可能是 Visual Studio 的错误。 Please reference the following post to see if it applies to the VS version you are using: Unable to fetch source information for Test Method请参考以下帖子,看看它是否适用于您正在使用的 VS 版本: 无法获取测试方法的源信息

Is this even correct?这甚至正确吗? I am so new to C# I don't feel confident.我对 C# 很陌生,我没有信心。

This is a very broad question.这是一个非常广泛的问题。 Consider getting the book I mentioned above.考虑买我上面提到的那本书。 Find an open source C# project that has comprehensive Unit Tests already and go READ THAT TEST CODE.找到一个已经有全面的单元测试的开源 C# 项目,然后阅读该测试代码。

This is an ideal way to expose yourself to ideas and idioms of Unit Testing.这是让自己接触单元测试的想法和习语的理想方式。

Keep in mind that making mistakes is a great way to learn and build confidence.请记住,犯错是学习和建立信心的好方法。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM