简体   繁体   English

动态更改类属性参数

[英]Change class attributes parameters dynamically

how can i change the attributes parameters dynamically(in run time) in the following code( for TestFixture and TestConfiguration): 我如何在以下代码(用于TestFixture和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) 我需要在运行时更改TestFixture和TestConfiguration的属性参数。(不使用const参数)

how can i change it by reflection or annotation? 如何通过反射或注释更改它?

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 . 只要您在类,方法或任何成员上具有属性,就可以随时使用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 . 因此,当您构造函数提供参数时,其值将绑定到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. 更改属性值时, 不会以任何方式通知 NUnit,因此它将不会重新创建您的测试,甚至不会修改已经存在的测试。 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? 那么当您在运行时更改TestFixture的值时应该怎么办? Shell all tests be re-run with the new values? 使用新值重新运行Shell所有测试? Or only those that have not been run so far? 还是只有到目前为止尚未运行的那些?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM