I've been using NUnit for testing and I'm really fond of test cases. In NUnit you can easily set each test name in the test case using the SetName function in a TestCaseData class.
Does xUnit have a similar function for this?
Currently I can only see one test in the test explorer even tho I have 6 tests in the test case.
xUnit test
public class LogHandler : TestBase
{
private ILogger _logger;
public LogHandler()
{
//Arrange
LogAppSettings logAppSettings = GetAppSettings<LogAppSettings>("Log");
IOptions<LogAppSettings> options = Options.Create(logAppSettings);
LogService logService = new LogService(new Mock<IIdentityService>().Object, options);
LogProvider logProvider = new LogProvider(logService);
_logger = logProvider.CreateLogger(null);
}
public static IEnumerable<object[]> TestCases => new[]
{
new object[] { LogLevel.Critical,
new EventId(),
new Exception(),
1 },
new object[] { LogLevel.Error,
new EventId(),
new Exception(),
1 },
new object[] { LogLevel.Warning,
new EventId(),
new Exception(),
0 },
new object[] { LogLevel.Information,
new EventId(),
new Exception(),
0 },
new object[] { LogLevel.Debug,
new EventId(),
new Exception(),
0 },
new object[] { LogLevel.Trace,
new EventId(),
new Exception(),
0 },
new object[] { LogLevel.None,
new EventId(),
new Exception(),
0 }
};
[Theory, MemberData(nameof(TestCases))]
public void Test(LogLevel logLevel, EventId eventId, Exception exception, int count)
{
//Act
_logger.Log<object>(logLevel, eventId, null, exception, null);
//Assert
int exceptionCount = Database.Exception.Count();
Assert.Equal(exceptionCount, count);
}
}
xUnit test window
Should be 6 tests here instead of one! (ignore GetOrganisationStatuses).
NUnit test case
public static IEnumerable TestDatabaseCases
{
get
{
yield return new TestCaseData(LogLevel.Critical,
new EventId(1),
new Exception("Exception"),
0,
1).SetName("InsertException_Should_Insert_When_LogLevel_Critical");
yield return new TestCaseData(LogLevel.Error,
new EventId(1),
new Exception("Exception"),
0,
1).SetName("InsertException_Should_Insert_When_LogLevel_Error");
yield return new TestCaseData(LogLevel.Warning,
new EventId(1),
new Exception("Exception"),
0,
0).SetName("InsertException_Should_Not_Insert_When_LogLevel_Warning");
yield return new TestCaseData(LogLevel.Information,
new EventId(1),
new Exception("Exception"),
0,
0).SetName("InsertException_Should_Not_Insert_When_LogLevel_Information");
yield return new TestCaseData(LogLevel.Debug,
new EventId(1),
new Exception("Exception"),
0,
0).SetName("InsertException_Should_Not_Insert_When_LogLevel_Debug");
}
}
NUnit test window
This is what I want in xUnit!
How do I in xUnit set a name for each test in the test case?