Passing a parameter to MSTests using MSBuild

Passing a parameter to MSTests using MSBuild

Ask Question

11 years ago

10 years, 9 months ago

2k times



I’ve got an issue that was wondering if could be solved in a particular way.

I would like to be able to pass a parameter or set some kind of variable in an MSBuild script that will be run on a TeamBuild server.

This parameter would be used as a condition in the setup of a TestFixture in MSTest to decided which concrete implementation of a class to be used. It would be a mock version when running on the build server, and a reference to a physical asset when running on a developer machine.

Is this easily possible? I could set an environment variable but would prefer if there was something specific in MSTest and MSBuild that could be used.

msbuild conditional mstest team-build

share|improve this question

edited Mar 10 ’09 at 4:01


20k1515 gold badges6868 silver badges9595 bronze badges

asked Jan 20 ’09 at 10:42


2,79633 gold badges2525 silver badges2626 bronze badges


I’m interested this question in the context of deployment package validation (…). We all know if you passing parameters to the test fixture, it is not a unit test. However, it is also valuable to automate tests at levels above unit tests.

– Precipitous
May 9 ’09 at 23:08

add a comment

2 Answers





As a xUnit guideline, tests should not take in parameters. They should just run without someone having to configure them.

public void TestMethodName()

Your need seems to be more towards dependency injection. For which frameworks like Spring.Net are a better fit.

From your comment, it seems all you require is a switch similar to a #define BUILD. Try Conditional Compilation symbols (Project Settings>Build) coupled with a ReplaceCollaboratorsForBuildServer method that is decorated with the ConditionalAttribute and called at the end of your testFixture Setup method.

share|improve this answer

edited Jan 20 ’09 at 11:05

answered Jan 20 ’09 at 10:49


120k4343 gold badges210210 silver badges290290 bronze badges

We’re using Unity, but we’re wanting to avoid having to use elaborate configuration files to set up the tests, hence why one piece of information stating that it was running on a build server which could be accessed from code would be sufficient.

– Campbell
Jan 20 ’09 at 10:50

add a comment


The easiest way to do this that I have found is to write configuration files. There are MsBuild community tasks that make this possible.

share|improve this answer

answered May 11 ’09 at 7:18


4,65544 gold badges2323 silver badges3232 bronze badges

add a comment

Your Answer

Thanks for contributing an answer to Stack Overflow!Please be sure to answer the question. Provide details and share your research!But avoid …Asking for help, clarification, or responding to other answers.Making statements based on opinion; back them up with references or personal experience.To learn more, see our tips on writing great answers.

Draft saved
Draft discarded

Sign up or log in

Sign up using Google

Sign up using Facebook

Sign up using Email and Password


Post as a guest


Required, but never shown

Post Your Answer


By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you’re looking for? Browse other questions tagged msbuild conditional mstest team-build or ask your own question.


When laziness is efficient: Make the most of your command line

Ben Popper is the worst coder in the world: Something awry with my array

Featured on Meta

TLS 1.0 and TLS 1.1 removal for Stack Exchange services

Did Stack Exchange cut the number of negative comments nearly in half between…

An account of my meeting with the Stack Overflow management team



How to validate deployment packages created by msbuild? (preferably using mstest or nunit)


2Running MSTest Unit tests As part of TeamBuild3NAnt + MSBuild (4.0) == MSBuild launch failure w/directory not found error1Msbuild: transforming one itemgroup into another itemgroup with a different structure1Specify MSTest parameters in MSBuild6Run deployment script from TFS build workflow on remote system using MSTest agent only0Running selenium tests on an MSBuild build server117Team city unmet requirement: MSBuildTools12.0_x86_Path exists0Alternatives to using MSI’s – MSBuild?2How to force MSBuild version?

Hot Network Questions

What does the horizontal bar indicate in the DMM display?

Why can’t Ditto copy eyes properly?

Why smashing a sum change an horizontal spacing?

Could New Horizons take a “Pale Blue Dot”-like image this year?

Removing a nested list associated with a key and inserting into a new list

Why don’t organs have weighted keys?

Is this a game to you?

Is the conflict between Captain America and Tony Stark in “The Avengers” Blue Collar versus Educated Elite?

create empty img with dd so that it its sectors are of 4096bytes rather than 512

Why I get “tar: Cowardly refusing to create an empty archive”? when trying to backup the whole server

Using two tires on one wheel to prevent punctures?

Reversed Iota’s

Can I ask a cop, “What happened?”

I have a 2nd broken spoke after 300km on a new bike, is it the wheel or me?

I like my new job but I’m always late and I skip work often. How to avoid this?

How does the file command distinguish text and LaTeX files?

Corner modelling technique

If sound passes through material, vibration is produced. So are electromagnetic waves produced too?

What prevents me from setting up an A name entry for “” to whatever IP I want?

PhD manuscript with grammatical errors and informal phrases

Why is the asteroid belt shaped like a triangle?

Maybe error in ESTA question “Have you ever been issued a passport or ID for travel by any other country?”

How did Atari lose money on home computers?

Crooked (Not Parallel with Wall) Electrical Receptacle

more hot questions

Question feed

Subscribe to RSS

Question feed
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.


Leave a Reply

Your email address will not be published. Required fields are marked *