my test fails because of : Message: The sourceName specified on a TestCaseSourceAttribute must refer to a static field, property or method.
This is my Code:
const double MAX_DELTA = 0.01;
Qv_ges qv_ges_NE;
double Sum_Qv_ges_R_FL;
Qv_ges Qv_ges_Quer;
[SetUp]
public void init()
{
qv_ges_NE = Din1946.Calc_Qv_ges_NE(205.7d);
Sum_Qv_ges_R_FL = 15d + 15d + 15d + 15d + 15d + 10d + 10d + 10d + 10d + 10d + 10d + 10d;
Qv_ges_Quer = Din1946.Calc_Qv_ges_Quer(qv_ges_NE, Sum_Qv_ges_R_FL);
}
public IEnumerable<TestCaseData> TestCases_A()
{
yield return new TestCaseData(72.5, Qv_ges_Quer.FL.Value, MAX_DELTA);
yield return new TestCaseData(169.17, Qv_ges_Quer.RL.Value, MAX_DELTA);
yield return new TestCaseData(241.67, Qv_ges_Quer.NL.Value, MAX_DELTA);
yield return new TestCaseData(314.17, Qv_ges_Quer.IL.Value, MAX_DELTA);
}
[TestCaseSource("TestCases_A")]
public void MethodA(double expected, double value, double latitude)
{ Assert.AreEqual(expected, value, latitude);}
I used only static testcase-methods, but now I need data like:
Qv_ges_Quer.IL.Value
, Qv_ges_Quer.FL.Value
.... so I deleted static
How can I use a non static testcase? I also noticed by debugging that it doesn't enter the SetUp at first
This is my old code which I want to reorginze, mybe you know another/better way then the way above:
public void MethodA(){
Qv_ges qv_ges_NE = Din1946.Calc_Qv_ges_NE(205.7d);
double Sum_Qv_ges_R_FL = 15d + 15d + 15d + 15d + 15d + 10d + 10d + 10d + 10d + 10d + 10d + 10d;
Qv_ges Qv_ges_Quer = Din1946.Calc_Qv_ges_Quer(qv_ges_NE, Sum_Qv_ges_R_FL);
Assert.AreEqual(72.5, Qv_ges_Quer.FL.Value, MAX_DELTA);
Assert.AreEqual(169.17, Qv_ges_Quer.RL.Value, MAX_DELTA);
Assert.AreEqual(241.67, Qv_ges_Quer.NL.Value, MAX_DELTA);
Assert.AreEqual(314.17, Qv_ges_Quer.IL.Value, MAX_DELTA);
}
🧟♀️ Zombie response, but better late than never. 🧟
Another way to accomplish this is to have your test case data source return a function object that accepts the non-static members you need as its parameter(s). Then your test calls that to create the data that you wish NUnit could pass into you.
In your case, that looks something like:
private static IEnumerable<TestCaseData> GetTestDataA()
{
yield return new TestCaseData(72.5, new Func<Qv_ges, double>( qvGesQuer => qvGesQuer.FL.Value ), MAX_DELTA);
yield return new TestCaseData(169.17, new Func<Qv_ges, double>( qvGesQuer => qvGesQuer.RL.Value ), MAX_DELTA);
yield return new TestCaseData(241.67, new Func<Qv_ges, double>( qvGesQuer => qvGesQuer.NL.Value ), MAX_DELTA);
yield return new TestCaseData(314.17, new Func<Qv_ges, double>( qvGesQuer => qvGesQuer.IL.Value ), MAX_DELTA);
}
[TestCaseSource( nameof(GetTestDataA) )]
public void MethodA( double expected, Func<Qv_ges, double> getValue, double latitude)
{
Assert.AreEqual( expected, getValue( Qv_ges_Quer ), latitude );
}
If you need multiple parameters, add them to the functor's and lambda's parameters or consider passing in this
instead. If you need multiple return values, make the function object return a tuple:
new Func<Qv_ges, (double, double)>( qvGesQuer => (qvGesQuer.RL.Value, qvGesQuer.IL.Value) )
A different approach is to pass in nameof()
strings as the test params and use reflection to get the values of those parameters.
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