1

how can i change the attributes parameters dynamically(in run time) in the following code( for TestFixture and TestConfiguration):

[
    TestFixture("Setup 1"),
    TestConfiguration("http://spiratest", "rin", "rin", 30, 924, 2577, 
    TestConfigurationAttribute.RunnerName.NUnit)
]
    public  class SampleTestFixture
    {
        protected static int testFixtureState = 1;

        [TestFixtureSetUp]
        public void FixureInit()
        {
            //Set the state to 2
            testFixtureState = 2;
        }

        [SetUp]
        public void Init()
        {
            //Do Nothing
        }

        /// <summary>
        /// Sample test that asserts a failure
        /// </summary>
        [
        Test,
        TestCase(41681)
        ]
        public void _01_SampleFailure()
        {
            //Verify the state
            Assert.AreEqual (2, testFixtureState, "*Real Error*: State not persisted");

            //Failure Assertion
            Assert.AreEqual (1, 1, "Failed as Expected");
        }   
}

i need to change the attributes parameters for TestFixture and TestConfiguration on RunTime.(with no using const parameters)

how can i change it by reflection or annotation?

8
  • You want to change the tests name? Why do you need this? I doubt this is possible because even if you could change the values NUnit won´t recognize them as the tests are already created and run. Commented Jul 4, 2016 at 8:28
  • i need to change the parameter in the TestFixture instead of "Setup 1" Commented Jul 4, 2016 at 8:31
  • i need to change the parameter in the TestFixture instead of "Setup 1" to be a string parameters that will be assign in runtime. Commented Jul 4, 2016 at 8:37
  • So what should happen when you change the value of the TestFixture at runtime? Shell all tests be re-run with the new values? Or only those that have not been run so far? Your question makes not much sense. Commented Jul 4, 2016 at 8:49
  • Possible duplicate of Nunit parameterised TestFixtures with parameters set at runtime? ? Commented Jul 4, 2016 at 8:53

1 Answer 1

1

I doubt what you want is possible. Whenever you have attributes on a class, method or any member those are handled using reflection at any time using GetCustomAttributes.

// find the fixtures
// ...
// provide the attributes and create the fixture
var newTestInstance = Activator.CreateInstance(typeof(SampleTestFixture), theParams)

When you call the member with those attributes you provide the information within the attribute to that member or constructor, however the member (or constructor) has already been called with the values provided by those attributes. What you want is therefor similar to this:

class MyClass {
    int MyInt;
    MyClass(int param)
    {
        MyInt = param;
    }
} 

So when you provide the parameter to the constructor its value is bound to MyInt. When you change the attributes value NUnit is not notified in any way, so it won´t re-create your test or even modify the already existing one. Both would be harmful. In the first place you would create a completely new test. In the second case you would have to determine which tests have already been run and re-run those with the modifed value.

So what should happen when you change the value of the TestFixture at runtime? Shell all tests be re-run with the new values? Or only those that have not been run so far?

Sign up to request clarification or add additional context in comments.

2 Comments

all tests should be run with the new value.
The answer is "it's not possible" - but really he probably has some more meaningful question he should be asking, the reason why he imagines he needs to do this. Whatever it is, dynamically changing the code is probably not the answer. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.