[英]Skip unit-test if the user is not allowed to do it
We are building our unit-tests with Visual Studio 10 integrated MS unit-tests. 我们正在使用Visual Studio 10集成的MS单元测试来构建单元测试。
Some functions in our application do only work right, if the user that tries to access this function has the right privileges - like being administrator of the computer (or even evaluated administrator (depending on the target system). For example, we are creating new performance counters - for that you do need to be administrator. 如果尝试访问此功能的用户具有正确的特权,例如我们是计算机的管理员(甚至是经过评估的管理员(取决于目标系统),则我们应用程序中的某些功能只能正常工作)。例如,我们正在创建新的性能计数器-为此,您需要成为管理员。
Now, I want to test those functions with unit tests. 现在,我想用单元测试来测试那些功能。 When I start VS as administrator, all is good.
当我以管理员身份启动VS时,一切都很好。 But we are also have an automated build system (Jenkins) that runs the unit tests with a normal user.
但是,我们还有一个自动构建系统(Jenkins),该系统可以由普通用户运行单元测试。 The question is, is there a nice way (like a method attribute) to skip tests when the user that runs the unit tests does not have the privileges to run them.
问题是,当运行单元测试的用户没有权限运行测试时,是否有一种很好的方法(如method属性)跳过测试。
EDIT 1 编辑1
Changing configurations based on target machines does not really satisfy me. 基于目标计算机更改配置并不能真正满足我的需求。 I just found the TestClassExtensionAttribute Class and some nice tutorial how to implement it.
我刚刚找到了TestClassExtensionAttribute类,并找到了一些不错的教程来实现它。 I will try to go that way now, since it is much easier when developing tests.
我现在将尝试采用这种方式,因为在开发测试时会容易得多。
You could use TestCategories on your test. 您可以在测试中使用TestCategories。 Just mark your test with:
只需将您的测试标记为:
[TestCategory("Admin") TestMethod()]
public Void DebitTest()
{
}
And then exclude category: 然后排除类别:
mstest /testcontainer:MyTestprojectName.dll /category:"!Admin"
You can use multiple categories on each test 您可以在每个测试中使用多个类别
See more here: http://msdn.microsoft.com/en-us/library/dd286683.aspx 在此处查看更多信息: http : //msdn.microsoft.com/en-us/library/dd286683.aspx
You can also put all relevant tests in a Test list and then just run this list, like this: 您还可以将所有相关测试放入“测试”列表中,然后像下面这样运行该列表:
http://msdn.microsoft.com/en-us/library/ms182461(v=vs.80).aspx http://msdn.microsoft.com/en-us/library/ms182461(v=vs.80).aspx
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.