首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >动态更改类属性参数

动态更改类属性参数
EN

Stack Overflow用户
提问于 2016-07-04 08:24:48
回答 1查看 807关注 0票数 1

如何在以下代码(对于TestFixture和TestConfiguration)中动态更改属性参数(在运行时):

代码语言:javascript
复制
[
    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");
        }   
}

我需要更改TestFixture和TestConfiguration在RunTime上的属性参数。(不使用const参数)

我如何通过反射或注释来改变它?

EN

回答 1

Stack Overflow用户

发布于 2016-07-04 08:39:31

我怀疑你想要什么是可能的。每当您在类、方法或任何成员上拥有属性时,任何时候都可以使用GetCustomAttributes使用反射来处理这些属性。

代码语言:javascript
复制
// find the fixtures
// ...
// provide the attributes and create the fixture
var newTestInstance = Activator.CreateInstance(typeof(SampleTestFixture), theParams)

当使用这些属性调用成员时,可以将属性中的信息提供给该成员或构造函数,但是已经用这些属性提供的值调用了成员(或构造函数)。你想要的与此类似:

代码语言:javascript
复制
class MyClass {
    int MyInt;
    MyClass(int param)
    {
        MyInt = param;
    }
} 

因此,当您向构造函数提供参数时,它的值被绑定到MyInt。当您更改属性值时,NUnit是,没有以任何方式通知,所以它不会重新创建您的测试,甚至不会修改已经存在的测试。两者都是有害的。首先,您将创建一个全新的测试。在第二种情况下,您必须确定哪些测试已经运行,并重新运行那些具有修改值的测试。

那么,当您在运行时更改TestFixture的值时,应该发生什么呢?用新值重新运行所有测试?还是只有那些到目前为止还没有运行的?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38179973

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档