简体   繁体   中英

MSTest - Hide some unit tests from build server

I have three unit tests that cannot pass when run from the build server—they rely on the login credentials of the user who is running the tests.

Is there any way (attribute???) I can hide these three tests from the build server, and run all the others?

Our build-server expert tells me that generating a vsmdi file that excludes those tests will do the trick, but I'm not sure how to do that.

I know I can just put those three tests into a new project, and have our build-server admin explicitly exclude it, but I'd really love to be able to just use a simple attribute on the offending tests.

You can tag the tests with a category, and then run tests based on category.

[TestCategory("RequiresLoginCredentials")]
public void TestMethod() { ... }

When you run mstest , you can specify /category:"!RequiresLoginCredentials"

Others answers are old.

In modern visual studio (2012 and above), tests run with vstest and not mstest.

New command line parameter is /TestCaseFilter:"TestCategory!=Nightly" as explained in this article.

There is an IgnoreAttribute . The post also lists the other approaches.

Open Test->Windows->Test List Editor.

There you can include / hide tests

I figured out how to filter the tests by category in the build definition of VS 2012. I couldn't find this information anywhere else.

in the Test Case Filter field under Test Source, under Automated Tests, under the Build process parameters, in the Process tab you need to write TestCategory=MyTestCategory (no quotes anywhere)

Then in the test source file you need to add the TestCategory attribute. I have seen a few ways to do this but what works for me is adding it in the same attribute as the TestMethod as follows.

[TestCategory("MyTestCategory"), TestMethod()]

Here you do need the quotes

When I run unit tests from VS build definition (which is not exactly MSTest), in the Criteria tab of Automated Tests property I specify:

TestCategory!=MyTestCategory

All tests with category MyTestCategory got skipped.

My preferred way to do that is to have 2 kinds of test projects in my solution : one for unit tests which can be executed from any context and should always pass and another one with integration tests that require a particular context to run properly (user credential, database, web services etc.). My test projects are using a naming convention (ex : businessLogic.UnitTests vs businessLogic.IntegrationTests) and I configure my build server to only run unit tests (*.UnitTests). This way, I don't have to comment IgnoreAttributes if I want to run the Integration Tests and I found it easier than editing test list.

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