A very quick blog post tonight. This came about when I wanted to setup a new Unit Test project for an existing .NET 6 solution. The problem came with setting up the project package references and I couldn't remember what to add. So for future reference, the packages I added from NuGet were:
- NUnit - The main unit-testing framework
- NUnit3TestAdapter - This is used for running NUnit 3.x tests in Visual Studio
- Microsoft.NET.Test.SDK - MSBuild targets and properties for building .NET test projects
The project was created as a Class library (Console Application also works but it doesn't really make sense given it's not hosting its own executable)
Add a class and within this attribute the tests to be run with either [Test]
[Test]
public void Test_Something()
{
var expected = "Something";
var testValue = DoSomething();
Assert.That(testValue, Is.EqualTo(expected));
}
Or [TestCase], if you want to supply parameters
[TestCase("Test1", 1, 2)]
[TestCase("Test2", 2, 3)]
public void LatLong_To_Mercator(string testName, int input, int expected)
{
// Run test with parameters and Asset against expected
}
One last thing, the documentation says to add [TestFixture] to the class but this doesn't seem to be needed. The tests run anyway.
Ahha!
Beginning with NUnit 2.5, the TestFixture attribute is optional for non-parameterized, non-generic fixtures. So long as the class contains at least one method marked with the Test, TestCase or TestCaseSource attribute, it will be treated as a test fixture.
No comments:
Post a Comment