Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect if my tests are running on Visual Studio Team Services

I've wrote a lot of integration tests for an API I'm working on.

If I run the integration tests locally I want to self host the API and run the integration tests against it.

However I have a build definition on VS Team services that runs the integration test after the API was deployed, I want to run the tests against the deployed API, but to do that I have to change my code so it no longer self host it and instead tested against the deployed API.

Is there a way to detect at runtime where my tests are being run?

Thanks

like image 203
General Electric Avatar asked Oct 12 '25 11:10

General Electric


2 Answers

Just probe for one of the VSTS predefined build variables. For example:

if (Environment.GetEnvironmentVariable("SYSTEM_DEFINITIONID") != null) 
{
    Console.WriteLine("Running from VSTS...");
}
like image 152
Ohad Schneider Avatar answered Oct 15 '25 00:10

Ohad Schneider


If you are using VSTS Hosted Build agent to run the build and test, the machine name of these agents has a format like "TASKAGENTX-XXXX". You can use it (Environment.MachineName) to determine if the test is ran on VSTS or your local machine.

By the way, what is the difference between the testing run on your local machine and VSTS? Is it just a different URL? If yes, you can add a config file to your project and update your tests to read the URL in the config file for testing. And then use SlowCheetah to perform a transformation to switch the URL when build and test from VSTS.

like image 38
Eddie Chen - MSFT Avatar answered Oct 15 '25 01:10

Eddie Chen - MSFT