26

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.

7 Answers 7

35

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"

Sign up to request clarification or add additional context in comments.

3 Comments

That's how I do it in NUnit too.
Perfect answer. Just one small thing: are you sure it's not TestCategory, instead of Category?
David, here's a closely-related follow-up question, if you're interested: stackoverflow.com/questions/6061282/…
3

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

Comments

3

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.

Comments

2

Open Test->Windows->Test List Editor.

There you can include / hide tests

Comments

1

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

Comments

1

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.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.