I want my unit-tests based on NUnit
framework named a bit more human readable in Visual Studio test explorer.
For example instead of having Test_Case_1
or TestCase1
I would better have something like Test Case #1, Category: First, Category: Second
(by assigning values from [Category]
attributes as well), with spaces and characters not allowed in methods names.
I know it's out-of-the-box possible in xUnit
, but I cannot involve it, since I'm using my customizations that I was not able to implement using xUnit
framework.
Is it possible to rewrite unit test display name with NUnit
? So far I can see, that FullName
field of TestDetail
has private setter.
Is any other ways or approaches change display name for NUnit
tests?
Naming your tests The name of your test should consist of three parts: The name of the method being tested. The scenario under which it's being tested. The expected behavior when the scenario is invoked.
Execution speed MsTest is a native unit testing library that comes with Visual Studio from Microsoft. NUnit is an extra Nuget package that needs to be installed on top and interact with the APIs of Visual Studio. Nunit likely doesn't have direct integration into the native APIs like MsTest does.
Mock objects are objects that replace the real objects and return hard-coded values. This helps test the class in isolation. There is a difference between mocks and stubs. A stub is used to simulate an object.
I wanted to change the test name of a parameterized NUnit test programmatically and dynamically, i.e. based on input files in a test data folder.
It took a couple of adjustments, but this works:
[Test, TestCaseSource(nameof(GetSites))]
public void TestForEveryFile(object ignored, FileInfo testFile) {
// actual test using 'testFile'
}
public static IEnumerable<TestCaseData> GetSites()
{
foreach (string testFile in Directory.EnumerateFiles("TestData"))
{
FileInfo fileInfo = new FileInfo(testFile);
// Pass '' as first argument to TestCaseData to suppress the default test name
// (seems to be necessary even if TestName is set)
var testCase = new TestCaseData("", fileInfo)
{
// Use the short file name as test name.
// As dots (.) would indicate a test hierarchy, we replace them with '-'.
TestName = fileInfo.Name.Replace(".", "-")
};
yield return testCase;
}
}
You can create your own Name attribute:
// I used the same namespace for convenience
namespace NUnit.Framework
{
public class NameAttribute : NUnitAttribute, IApplyToTest
{
public NameAttribute(string name)
{
Name = name;
}
public string Name { get; set; }
public void ApplyToTest(Test test)
{
test.Properties.Add("Name", Name);
}
}
}
You then use it like a regular NUnit property (Just like Category and Description).
[Test, Name("My Awesome Test"), Category("Cool.Tests"), Description("All cool tests")]
public void Test313()
{
// Do something
}
You can see the data in your TestContext:
if (TestContext.CurrentContext.Test.Properties.ContainsKey("Name"))
{
name = TestContext.CurrentContext.Test.Properties.Get("Name") as string;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With