Naming individual tests with arguments from TestFixtureData

336 views
Skip to first unread message

Josh Leonard

unread,
Jun 15, 2017, 6:38:22 PM6/15/17
to NUnit-Discuss
Hi All, 

I have a TestFixture that takes a TestFixtureData with two arguments, those arguments are stored in static attributes for later. I also have TestCaseSources that provide TestCaseData, and I rename the TestName property using the fixture attributes mentioned before. When I build the project the fixture creates tests based on the two arguments as expected, but the variables used for naming do not get iterated over. The name is always displayed as the first set of fixture arguments. 

I was wondering, shouldn't the test names iterate with the TestFixtureData arguments?

If not, does anyone know how to rename tests with fixture data? Using the Nunit naming template you are able to pull some information, but you're unable to get fixture data if you're calling from a test.

Thanks

Rob Prouse

unread,
Jun 15, 2017, 6:40:18 PM6/15/17
to NUnit-Discuss
Can you provide some example code so we can see what you mean?

--
You received this message because you are subscribed to the Google Groups "NUnit-Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nunit-discuss+unsubscribe@googlegroups.com.
To post to this group, send email to nunit-...@googlegroups.com.
Visit this group at https://groups.google.com/group/nunit-discuss.
For more options, visit https://groups.google.com/d/optout.

Josh Leonard

unread,
Jun 16, 2017, 8:37:50 AM6/16/17
to NUnit-Discuss
[TestFixture, TestFixtureSource(nameof(FixtureParms))]
public class TestClass
{
   private static bool fixtureAttribute1;
   private static int fixtureAttribute2;

   public TestClass(bool param1, int param2)
   {
     fixtureAttribute1 = param1;
     fixtureAttribute2 = param2;
   }

   public static IEnumerable FixtureParms
   {
     get
     {
       yield return new TestFixtureData(true, 0);
       yield return new TestFixtureData(true, 1);
       yield return new TestFixtureData(false, 2);
     }
   }

   public class NameProvider : IEnumerable
   {
      public IEnumerator GetEnumerator()
      {
         yield return new TestCaseData
          {
            TestName = $"{{m}}({fixtureAttribute1}, {fixtureAttribute2})"
          };
      }
   }

   [Test, TestCaseSource(typeof(NameProvider))]
   public void TestMethod()
   {
      ...
   }
}

The goal is to produce three test cases with the names TestMethod(True, 0), TestMethod(True, 1), and TestMethod(False, 2). However when I build/run it, all the 3 names become TestMethod(True,0).

Any feedback would be helpful. Thanks!


On Thursday, June 15, 2017 at 5:40:18 PM UTC-5, Rob Prouse wrote:
Can you provide some example code so we can see what you mean?
On Thu, Jun 15, 2017 at 5:36 PM, Josh Leonard <josh.leo...@gmail.com> wrote:
Hi All, 

I have a TestFixture that takes a TestFixtureData with two arguments, those arguments are stored in static attributes for later. I also have TestCaseSources that provide TestCaseData, and I rename the TestName property using the fixture attributes mentioned before. When I build the project the fixture creates tests based on the two arguments as expected, but the variables used for naming do not get iterated over. The name is always displayed as the first set of fixture arguments. 

I was wondering, shouldn't the test names iterate with the TestFixtureData arguments?

If not, does anyone know how to rename tests with fixture data? Using the Nunit naming template you are able to pull some information, but you're unable to get fixture data if you're calling from a test.

Thanks

--
You received this message because you are subscribed to the Google Groups "NUnit-Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nunit-discus...@googlegroups.com.

Charlie Poole

unread,
Jun 17, 2017, 7:52:31 AM6/17/17
to NUnit-Discuss
The simplest way to explain this is that the names are all created before the tests are run.

The sequence of events is...

1. Load tests
  * Execute FixtureParms
  * For each fixture, create and use the NameProvider to generate test cases.
2. Run tests for each fixture instance
  * Execute constructor
  * Execute TestMethod
 
Your test case names are taken from fields that have not yet been initialized.

Of course, you could avoid this by parameterizing either the method or the fixture, rather than both. Can you explain why you need to set it up in this more complicated way?



To unsubscribe from this group and stop receiving emails from it, send an email to nunit-discuss+unsubscribe@googlegroups.com.

Josh Leonard

unread,
Jun 20, 2017, 9:16:50 AM6/20/17
to NUnit-Discuss
Its mainly for reporting. 
All of the parameters are on the fixture level but when the tests get recorded only the test name is shown, resulting in a list of "TestMethod" which is difficult to distinguish which test has which parameter set. I'm just curious if it can be done at all? I moved the fixture parameters to each test as a workaround, but it's not ideal because there are multiple tests per fixture. 

Thanks for your feedback.

Charlie Poole

unread,
Jun 20, 2017, 11:03:05 AM6/20/17
to NUnit-Discuss
NUnit could, of course, do it by supplying you with a % argument that referenced the fixture args. For a more general solution, we could figure out a way to set the name dynamically, at the time of execution. You could create an issue to see if we would accept a PR. Realistically, I'm fairly sure we would never get to it ourselves. :-(

In the future, we hope to have dynamic test case and test fixture data sources, which _would_ run at a later point, where the data is available. That's a bit farther down the road.

With what is currently available, there is no easy way. Best I can think of is to create your own Attribute. You might be able to derive it from test case source or - if you don't have enough member access that way - you can implement it yourself on the model of TestCaseSource. Either way, you would have access to the test case and it's parent fixture and could __probably__ set the name yourself.

I said "probably" because this is just me fantasizing about what I would try next - I can't say it will actually work. :-(



To unsubscribe from this group and stop receiving emails from it, send an email to nunit-discuss+unsubscribe@googlegroups.com.

Charlie Poole

unread,
Jun 20, 2017, 11:08:12 AM6/20/17
to NUnit-Discuss
A second thought on the last... Using TestCaseSource is something that you tried because it's available. It's not what you want. You want an attribute that sets the name of a test, so that's what you should create.

The attrtibute would have to implement IApplyToTest. The ApplyToTest method is called when NUnit is loading the test. When it is called, the parent fixture has already been created and has a property with it's arguments. You can do whatever you want to the test at that point, without worrying about what is available. (Note test and fixture here do not refer to your code, but to internal NUnit structures).

Good luck!

Josh Leonard

unread,
Jun 20, 2017, 1:17:37 PM6/20/17
to NUnit-Discuss
I will try that. Thank you for all your help!
Reply all
Reply to author
Forward
0 new messages