I want to be able to run tests from test explorer within visual studio by passing command-line arguments. I want to do this in order to be able to debug tests by setting breakpoints, which is easier for me to debug visually.
Currently, this is how I run tests from command-line:
PS> $env Browsestack_automation='false'; dotnet test
How can I achieve this?
@Jonathan Dodds, this is the .runsettings file that I’ve added to the root of my project:
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<!-- Configurations that affect the Test Framework -->
<RunConfiguration>
<!-- Use 0 for maximum process-level parallelization. This does not force parallelization within the test DLL (on the thread-level). You can also change it from the Test menu; choose "Run tests in parallel". Unchecked = 1 (only 1), checked = 0 (max). -->
<MaxCpuCount>0</MaxCpuCount>
<!-- Path relative to directory that contains .runsettings file-->
<ResultsDirectory>.TestResults</ResultsDirectory>
<!-- Omit the whole tag for auto-detection. -->
<!-- [x86] or x64, ARM, ARM64, s390x -->
<!-- You can also change it from the Test menu; choose "Processor Architecture for AnyCPU Projects" -->
<!-- TargetPlatform>x86</TargetPlatform -->
<!-- Any TargetFramework moniker or omit the whole tag for auto-detection. -->
<!-- net48, [net40], net6.0, net5.0, netcoreapp3.1, uap10.0 etc. -->
<TargetFrameworkVersion>net7.0</TargetFrameworkVersion>
<!-- Path to Test Adapters -->
<!-- TestAdaptersPaths>%SystemDrive%Tempfoo;%SystemDrive%Tempbar</TestAdaptersPaths -->
<!-- TestCaseFilter expression -->
<!-- TestCaseFilter>(TestCategory == BrowserStack)</TestCaseFilter -->
<!-- TestSessionTimeout was introduced in Visual Studio 2017 version 15.5 -->
<!-- Specify timeout in milliseconds. A valid value should be greater than 0 -->
<TestSessionTimeout>10000</TestSessionTimeout>
<!-- true or false -->
<!-- Value that specifies the exit code when no tests are discovered -->
<TreatNoTestsAsError>true</TreatNoTestsAsError>
<EnvironmentVariables>
<!-- List of environment variables we want to set--> <BROWSERSTACK_AUTOMATION>false</BROWSERSTACK_AUTOMATION>
</EnvironmentVariables>
</RunConfiguration>
</RunSettings>
2
Answers
Quoting Jonathan Dodds’ comment:
This is because you are not passing command line arguments. You are using environment variables. In the command window, set the environment variables, and then start Visual Studio by running devenv.exe. To change the environment, close Visual Studio, change the environment, and then restart.
1. What does devenv.exe do?
Yes, devenv.exe allows you to open a new instance of Visual Studio
2. How to set environment variables without restarting Visual Studio?
Restarting Visual Studio is to make the set environment variables take effect.
In general, if you change environment variables after Visual Studio starts, those changes usually don’t take effect immediately in the currently running instance of Visual Studio. Visual Studio reads system and user environment variables when it starts, but for an already running process, environment variables generally remain unchanged during its lifetime. Therefore, there is currently no direct method that does not require restarting Visual Studio. For details, please refer to: Refresh environment variables for open VS solution
The example doesn’t show passing command line parameters. It shows the use of environment variables.
In your command window, set your environment variables and then launch Visual Studio by running devenv.exe. To change your environment, close Visual Studio, change the environment, and relaunch.
devenv.exe is Visual Studio. (If you examine a start menu or taskbar shortcut you will see a path that ends with
devenv.exe
.)When devenv.exe is started in the command prompt, it gets the current set of environment variables. After the devenv process is started, changing the variables in the command prompt process (or in Windows Settings) will have no effect.
Changing the Environment for a Test Run
Take a look at creating a .runsettings file. The Visual Studio Test Explorer is running a test runner. In the .runsettings file you can specify environment variables that will be passed to the test runner when Test Explorer starts the test runner process.
You will need two .runsettings files: one for local and one for BrowserStack. In the "Test" menu in Visual Studio you will be able to switch between the two .runsettings files.