Failing to pass parameter from NUnit.Engine TestPackage to TestFixture via TestContext.Parameters

431 views
Skip to first unread message

mile.st...@inplayer.com

unread,
May 10, 2018, 9:52:46 AM5/10/18
to NUnit-Discuss
Hi All,

I found a post where it was suggested that the TestContext can be used to retrieve params. in TestFixtures.
Here's the code:

            var testAssembly = Assembly.Load("Tests");

            var package = new TestPackage(testAssembly.Location);
            package.AddSetting("Working Directory", Environment.CurrentDirectory);

            //package.AddSetting("dbPath", "123456"); - tried like this, also not passing param.

            package.Settings.Add("dbPath","c:\aPathVariableHere");
                        
            // Prepare the engine
            var engine = TestEngineActivator.CreateInstance();
            
            using (ITestRunner runner = engine.GetRunner(package))
            {
                // Execute the tests 
                var tf = new TestFilter("<filter><cat>IntegrationTests</cat></filter>");  
                var result = runner.Run(null, tf);
                //...
            }

And awaiting it on the other side at the TestFixture:

    [TestFixture]
    [Category("IntegrationTests")]
    public class UnitTest1
    {
        private bool _dbName = TestContext.Parameters.Exists("dbPath"); // trying to see if it can be retrieved before any test starts 

        [Test]
        public void TestMethod1()
        {
            
            var a = TestContext.Parameters["dbPath"];
            Assert.IsNotNull(a);
            Assert.IsNotEmpty(a);
            Assert.IsTrue(_dbName);
        }
///...

Otherwise, the tests are running just fine, the filter works according documentation. 

Another way - The parameter might be passed in the ctor of the TestFixture(string parameter), but I cannot see how to set the param. at the runner code (the first piece of code/class);

Any tips on why the parameter is missing at TestContext.Parameters, or there's something that I'm doing wrong?


Mikkel Bundgaard

unread,
May 11, 2018, 3:23:32 AM5/11/18
to NUnit-Discuss
Hi 

One possibility is to create a dictionary (with strings as keys and values) with your settings and then add this dictionary under the key "TestParametersDictionary". Thus mimicking how the console transfers parameters from the commandline to the framework.

See the console for how it adds the parameters to the 

and the framework for how it extracts each element from the dictionary and insert it into the TestContext.Parameters

This is essentially the new version of https://groups.google.com/forum/#!topic/nunit-discuss/-J34KEXyZbA that explains how to do it before we introduced the dictionary.

Kind regards,
Mikkel

Charlie Poole

unread,
May 11, 2018, 4:55:52 AM5/11/18
to NUnit-Discuss
Actually it has always been advised that the constructor of a test fixture should do very little work, generally limited to setting constants.

That's because NUnit gives strong guarantees about how and when your test method, setup methods and teardown methods are called but NOT regarding the constructor. Your constructor may be called long before the start of execution and is sometimes even called multiple times. Therefore, you should place this initialization in a OneTimeSetUp method, so that the context is set up properly.

BTW, it could be suggested that this is a design flaw, but it is the design!


--
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.
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.

Mile Stoilovski

unread,
May 11, 2018, 6:38:14 AM5/11/18
to nunit-...@googlegroups.com
Great! 

works like a charm :)

Many thanks for the fast reply Mikkel, Charlie!

Kind Regards,
Mile




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.

--
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.

wayne vetrone

unread,
May 13, 2018, 8:36:48 AM5/13/18
to nunit-...@googlegroups.com
Is the  TestParametersDictionary available in the Listener Extension?

Charlie Poole

unread,
May 13, 2018, 10:07:51 AM5/13/18
to NUnit-Discuss
Engine extensions are completely outside the NUnit framework and are not part of your test at all. The listener extension receives and acts on messages __about__ your test but it's running asynchronously in a different AppDomain and (usually) process and TextContext has no meaning here.

Can you describe what you may want to do with the information? Since it's constant for a given test run, it's possible that we could make it available to extensions. Currently, there's not much that the extension can query from the engine so maybe it's a place to start.
To unsubscribe from this group and stop receiving emails from it, send an email to nunit-discus...@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.

--
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.

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.

--
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.

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.

--
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.
Reply all
Reply to author
Forward
0 new messages