简体   繁体   中英

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.

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. But we are also have an automated build system (Jenkins) that runs the unit tests with a normal user. 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.

EDIT 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. I will try to go that way now, since it is much easier when developing tests.

You could use TestCategories on your test. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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