I have just started to learn C# and want to start testing using NUnit. I thought I would start very simple and test a "Hello World" program, just to get me used to NUnit in general.
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
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:
static void Main {}
? Can't get source code location for : HelloWorldTests" -
what does this mean?
If it's relevant, I'm using Visual Studio Community on a MacBook. 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.
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'. 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 {}
?
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()
:
Console.WriteLine( HelloMessage() );
But this has nothing at all to do with NUnit or Unit Testing. This is just convention that is part of C# console applications. 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?
Are you compiling in DEBUG or RELEASE? A RELEASE compilation does not have debugging symbols available within it and could be the cause of this problem. Also you might be able to workaround this problem by choosing Run Unit Tests
rather than Debug Unit Tests
. Give it a try.
This may be a Visual Studio bug. 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
Is this even correct? I am so new to C# I don't feel confident.
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.
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.