nunit: Not able to use TestFixtureSource values in TestCaseSource object.
I am trying to use TestFixtureSource values and pass it in TestCaseSource but null value is passed.
namespace HybridFramework
{
[TestFixtureSource("FixtureArgs")]
public class ParameterExperiment
{
static string eq1;
static object[] FixtureArgs = {
new object[] { "Question"},
new object[] { "Answer"}
};
public ParameterExperiment(string eq1)
{
ParameterExperiment.eq1 = eq1;
}
[TestCaseSource("getFruit")]
public void TestMethod(string fruit)
{
// TODO: Add your test code here
Assert.Pass("Your first passing test"+fruit + eq1);
}
static object[] getFruit =
{
new object[] {"Apple" },
new object[] { "Orange" },
new object[] { ParameterExperiment.eq1 }
};
}
}
TestFixture “ParameterExperiment” has TestFixtureSource as “FixtureArgs” with two parameters 1)Question and 2)Answer
TestCase “TestMethod” has TestCaseSource as “getFruit” with three parameters 1)Apple 2)Orange 3)ParameterExperiment.eq1 (TestFixture Parameter)
Issue: If I am using eq1(TestFixture Parameter) in test method I am able to see TestFixture Parameter value passed but if I am using eq1 in getFruit, null value is passed.
Expected: ParameterExperiment(“Question”)
- TestMethod(“Apple”)
- TestMethod(“Orange”)
- TestMethod(“Question”)
ParameterExperiment(“Answer”)
- TestMethod(“Apple”)
- TestMethod(“Orange”)
- TestMethod(“Answer”)
Actual: ParameterExperiment(“Question”)
- TestMethod(“Apple”)
- TestMethod(“Orange”)
- TestMethod(null)
ParameterExperiment(“Answer”)
- TestMethod(“Apple”)
- TestMethod(“Orange”)
- TestMethod(null)
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 24 (16 by maintainers)
It’s an interesting case, better than any I’ve seen so far.
For many years, we’ve had the goal of dynamic test cases, generated right before we run them. That feature, if it existed, would do the job for you.
But making fixture args available when generating test cases might be simpler in this case.
As explained, you cannot pass a value from step 1 to step 2. Not without rewriting parts of NUnit, anyway.
However, you don’t need to because the value of a member field is available to your method without being passed as an argument.
Consider any class you have, whether a test class or not. If it has instance members - properties or fields - all the instance methods in that class have access to the value when they are executing. They also have access to their arguments, of course. If you need to combine the value of a member field with the value of an argument in some way, you do it inside the method.
You haven’t given any example of code you would like to write that uses both the name of the fruit and the value of
eq1. If the above is still not clear, please provide an example and I’ll try to answer in context.Thanks for the reply!! Could you please help to elaborate more on: “In order for each instance of the fixture to have access to different arguments, this should be an instance - not a static - field or property.”
So as per your suggestion if I make eq1 as not static I cannot pass it in TestCaseSource - getFruit as its static and in nunit the source name specified on a TestCaseSource attribute must refer to static field or method.
I need to pass argument value from Step 1 to Step 2
The sequence of events is as follows:
So the test method gets the argument that was saved at step 2. This is simply how it works.
If your test methods need access to the fixture constructor arguments, your constructor should save them in a field or property. In order for each instance of the fixture to have access to different arguments, this should be an instance - not a static - field or property.
Hope this helped! I’m closing this question but you can ask followup questions here if you need to and we’ll see them and answer.