Running nunit test with specific data - nunit

I have a test that takes in test data. When using nunit console app to run the test, is there a way I can specify the data to be used?
Eg:
[Test, TestCaseSource(typeof(TestData))]
public void ATest(string param1, int param2)
public class TestData : IEnumerable
{
public IEnumerator GetEnumerator()
{
yield return new object[] { "blah1 blah1", 1};
yield return new object[] { "blah2 blah2", 2};
}
}
I want to be able to run ATest with test data ["blah2 blah2", 2] only. If I run as follows:
nunit3-console.exe Tests.dll --test=ATest --workers=1 --noresult
it will run twice.

Just run...
nunit3-console.exe Tests.dll --test ATest("blah2 blah2", 2)
or
nunit3-console.exe Tests.dll --where "test~=blah2"
If that string is unique to all your tests.
Note that the first one may require some escaping of the quotes, depending on your operating system.

One way to do this would be through returning a TestCaseData object instead.
Something like this: (untested, so syntax might be a little off!)
[Test, TestCaseSource(typeof(TestData))]
public void ATest(string param1, int param2)
public IEnumerator GetEnumerator()
{
yield return new TestCaseData("blah1 blah1", 1).SetName("FirstTest");
yield return new TestCaseData("blah2 blah2", 2).SetName("SecondTest");
}
To run the first test, you would then use the command line:
nunit3-console.exe Tests.dll --test=YourNameSpace.ATest.FirstTest --workers=1 --noresult
Depending what you're doing, setting the category may be more suitable than the name. The docs page shows what's available: https://github.com/nunit/docs/wiki/TestCaseData

Related

Converting a xUnit test case using MemberData to nUnit

Let's say I have the following test case that has been written using xUnit:
public static IEnumerable<object[]> testValues = new List<object[]>
{
new object[] {new double?[] {0.0}, 0.0, 0.0},
};
[Theory]
[MemberData(nameof(testValues))]
public void Test1(double?[] values, double expectedQ1, double expectedQ3)
{
// Test code
}
How could I express the same unit test in nUnit instead of xUnit?
Note: The main problem here seems to be the use of MemberData, which for so far, I haven't been able to find an nUnit equivalent. What would be the correct way of expressing such unit test cases in nUnit?
Like this:
public static IEnumerable<object[]> testValues = new List<object[]>
{
new object[] {new double?[] {0.0}, 0.0, 0.0},
};
[TestCaseSource(nameof(testValues))]
public void Test1(double?[] values, double expectedQ1, double expectedQ3)
{
// Test code
}
Note that NUnit has TheoryAttribute but you don't want it here. In NUnit, a Theory is a bit more than just a parameterized test. You should read the docs to understand what it is before deciding if you need it. Of course, you should read up on TestCaseSourceAttribute as well. :-)
Other attributes in NUnit that allow data to be specified for a test case include TestCaseAttribute, ValuesAttribute, ValueSourceAttribute, RandomAttribute and RangeAttribute.

NUnit 3.2 TestSourceCase TargetParameterCountException

I am trying to upgrade from NUnit 2 to NUnit 3.
And I built a Data-Driven-Helper to read test case data from several type of data files.
I found that NUnit 3.2's TestCaseSource that can pass parameters can help improve my Data-Driven-Helper, however, the problem is it keeps tell me
"Message: System.Reflection.TargetParameterCountException: Parameter count mismatch."
Here are test codes:
static public IEnumerable GetCases(string a)
{
yield return new object[] { "1", 1 };
}
[TestCaseSource(typeof(BaseFixtureTest), "GetCases", new object[] {"a"})]
public void someTest(string Path, int deg)
{
//*** some test logic
}

Nunit3 how to change testcase name based on parameters passed from TestFixtureSource

I'm using NUnit 3.0 and TestFixtureSource to run test cases inside a fixture multiple times with different parameters/configurations (I do want to do this at TestFixture level). Simple example:
[TestFixtureSource(typeof (ConfigurationProvider))]
public class Fixture
{
public Fixture(Configuration configuration)
{
_configuration = configuration;
}
private Configuration _configuration;
[Test]
public void Test()
{
//do something with _configuration
Assert.Fail();
}
}
Let's say Test() fails for one of the configurations and succeeds for another. In the run report file and in Visual Studio's Test Explorer the name for both the failed and the succeeded runs will be displayed as just Test(), which doesn't tell me anything about which setup caused issues.
Is there a way to affect the test cases names in this situation (i.e. prefix its name per fixture run/configuration)? As a workaround I'm currently printing to the results output before each test case fires but I would rather avoid doing that.
Since NUnit 3.0 is in beta and this feature is fairly new I wasn't able to find anything in the docs. I found TestCaseData but I don't think it's tailored to be used with fixtures just yet (it's designed for test cases).
I can't find a way to change the testname, but it should not be neccessary, because NUnit3 constructs the testname by including a description of the testfixture.
The example class Fixture from the question can be used unchanged if the Configuration and ConfigurationProvider has an implementation like this:
public class Configuration
{
public string Description { get; }
public Configuration(string description)
{
Description = description;
}
public override string ToString()
{
return Description;
}
}
public class ConfigurationProvider : IEnumerable
{
public IEnumerator GetEnumerator()
{
yield return new Configuration("Foo");
yield return new Configuration("Bar");
yield return new Configuration("Baz");
}
}
The 'trick' is to make sure the constructor-parameter to the fixture is a string or has a ToString-method that gives a sensible description of the fixture.
If you are using NUnit 3 Test Adapter in Visual Studio, then the testfixtures will be displayed as Fixture(Foo), Fixture(Bar) and Fixture(Baz) so you can easily distinguish between their tests. The xml-output from nunit3-console.exe also uses descriptive names, fx: fullname=MyTests.Fixture(Bar).Test
<test-case id="0-1003" name="Test" fullname="MyTests.Fixture(Bar).Test" methodname="Test" classname="MyTests.Fixture" runstate="Runnable" result="Failed" ... >
<failure>
<message><![CDATA[]]></message>
<stack-trace><![CDATA[at MyTests.Fixture.Test() in ... ]]></stack-trace>
</failure>
...
</test-case>
One way to perform such actions is to have find and replace tokens in source code and dynamically build test libraries before execution using command line msbuild. High level steps are
Define test case names as sometest_TOKEN in source then using command line tools like fnr.exe replce _TOKEN with whatever you like. For example sometest_build2145.
Compile the dll with using msbuild for example msbuild /t:REbuild mytestproj.sln. Thereafter execute all test cases in mytestproj.dll.

TestNG: Test names in Eclipse plugin

Suppose I have a simple test like so:
public class SimpleTestFactory {
private int instanceNumber;
#Factory(dataProvider="provideTestData")
public SimpleTestFactory(int instanceNumber) {
this.instanceNumber = instanceNumber;
}
#DataProvider
public static Object[][] provideTestData() { return new Object[][] {{1},{2}}; }
#Test
public void testOne() { System.out.printf("Test 1, Instance %d%n", instanceNumber); }
}
When I run this in Eclipse, I get only one entry instead of two in the "all tests" tab; both test instances are "grouped together" under SimpleTestFactory -> testOne(). Furthermore, say that there is an if-block in testOne() that fails instance 2. Then, the results in the "all tests" tab, it may show (depending on whether instance 1 or instance 2 is first) a test passing with a stack trace or a test failing with no stack trace, as if the result of one instance is overwriting another.
How can I get it so that the two instances are shown separately? That is, I get something like SimpleTestFactory[1] -> testOne() and SimpleTestFactory[2] -> testOne()? What if I add in a second test, testTwo()?
Have your test implement org.testng.ITest and override getName() to return the name of the instance.

NUnit TestCaseSource pass value to factory

I'm using the NUnit 2.5.3 TestCaseSource attribute and creating a factory to generate my tests. Something like this:
[Test, TestCaseSource(typeof(TestCaseFactories), "VariableString")]
public void Does_Pass_Standard_Description_Tests(string text)
{
Item obj = new Item();
obj.Description = text;
}
My source is this:
public static IEnumerable<TestCaseData> VariableString
{
get
{
yield return new TestCaseData(string.Empty).Throws(typeof(PreconditionException))
.SetName("Does_Reject_Empty_Text");
yield return new TestCaseData(null).Throws(typeof(PreconditionException))
.SetName("Does_Reject_Null_Text");
yield return new TestCaseData(" ").Throws(typeof(PreconditionException))
.SetName("Does_Reject_Whitespace_Text");
}
}
What I need to be able to do is to add a maximum length check to the Variable String, but this maximum length is defined in the contracts in the class under test. In our case its a simple public struct:
public struct ItemLengths
{
public const int Description = 255;
}
I can't find any way of passing a value to the test case generator. I've tried static shared values and these are not picked up. I don't want to save stuff to a file, as then I'd need to regenerate this file every time the code changed.
I want to add the following line to my testcase:
yield return new TestCaseData(new string('A', MAX_LENGTH_HERE + 1))
.Throws(typeof(PreconditionException));
Something fairly simple in concept, but something I'm finding impossible to do. Any suggestions?
Change the parameter of your test as class instead of a string. Like so:
public class StringTest {
public string testString;
public int maxLength;
}
Then construct this class to pass as an argument to TestCaseData constructor. That way you can pass the string and any other arguments you like.
Another option is to make the test have 2 arguments of string and int.
Then for the TestCaseData( "mystring", 255). Did you realize they can have multiple arguments?
Wayne
I faced a similar problem like yours and ended up writing a small NUnit addin and a custom attribute that extends the NUnit TestCaseSourceAttribute. In my particular case I wasn't interested in passing parameters to the factory method but you could easily use the same technique to achieve what you want.
It wasn't all that hard and only required me to write something like three small classes. You can read more about my solution at: blackbox testing with nunit using a custom testcasesource.
PS. In order to use this technique you have to use NUnit 2.5 (at least) Good luck.