TLDR; 如何根据 app.config 文件中的值在 C# 中创建预处理指令?
我正在使用 Selenium 进行集成测试。我有两个测试方法类,负责测试应用程序的不同区域。我的测试类使用 [TestClass]
属性进行标识,我的测试方法使用 [TestMethod]
属性进行标识,如下所示:
[TestClass]
public class TestsForAreaX
{
[TestMethod]
public void TestFoo()
{
//Do test logic here
}
[TestMethod]
public void TestBar()
{
//Do test logic here
}
}
当我重建解决方案时,应用了 [TestMethod]
的所有方法都将出现在测试资源管理器中。
我现在想做的是向我的 app.config 添加一些标志,以有条件地应用 [TestMethod]
属性,以便我可以指定要运行的测试。原因是有些测试花费很长时间,我想排除它们,而不必直接摆弄方法属性。
我知道以下预处理指令:
#if DEBUG
//debug mode
#elif
//release mode
#endif
但这仅适用于构建配置,不适用于应用程序配置文件中的值,除非我弄错了?
最佳答案
编译后无法删除程序集的属性。也许您正在寻找类别:
[TestClass]
public class TestsForAreaX
{
[TestCategory("LongRunning"), TestMethod]
public void TestFoo()
{
//Do test logic here
}
[TestCategory("ShortRunning"), TestMethod]
public void TestBar()
{
//Do test logic here
}
} enter code here
然后您至少可以从命令行运行不同类别的测试,此站点解释了如何: http://msdn.microsoft.com/en-us/library/dd286683.aspx
关于C# - 从 app.config 有条件地将属性应用于方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22039094/