Create Batch file to run mstest for coded ui test automation - batch-file

Can someone help me out in creating Batch file to run the coded ui test using mstest?
I am currently running the coded ui test in the command prompt by running as administrator by using below lines in command prompt:
cd\
cd C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE
C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE>MSTest.exe /testcontainer:D:\Working\LOPS_Testing\LOPS6Automation\bin\LOPS6Automation.dll /test:Light_Oil_Price_System.Scripts.PricingScreen_Script.PricingScreenFunctionality
This works correctly.
But now I want to use the same in a batch file. Can some one help me out to create a batch file by using the above code?

Wrap paths with spaces between quotes.
I also used cd /D, that changes drive if necessary (for example if you were on drive D: you would need to change to C: before cd commands. With /D option this is done automatically)
Also, since you are already on the Common7\IDE folder, you don't need to write again the directory, you could just call MSTest.exe:
cd /D "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE"
MSTest.exe /testcontainer:D:\Working\LOPS_Testing\LOPS6Automation\bin\LOPS6Automation.dll /test:Light_Oil_Price_System.Scripts.PricingScreen_Script.PricingScreenFunctionality

Related

The using of visual studio compiler by cmd.exe + batch file

Could you help me please with the using of visual studio compiler by cmd.exe + batch file?
I need to create batch file that will be executed in cmd.exe (C:\WINDOWS\system32\cmd.exe).
The body of .bat-script is:
"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2017\Visual Studio Tools\VC\x64 Native Tools Command Prompt for VS 2017.lnk"
"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x64\cl.exe" D:\temp\ctemplate\2\helloexample.c
Means I want to prepare command line with visual studio environment and then I want to compile file helloexample.c.
The shrtcut/link contains the target: %comspec% /k "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat"
During the execution I see that only first line is handled.
The body of c-script is:
#include <stdio.h>
int main()
{
printf("Hello, World!");
return 0;
}
During the execution I see that only first line is handled.
If you want to run a batch file from another batch file you have to call it. Unlike an executable code run from a batch file, if you just run another batch file, the system does not return control to the calling batch file. That is why only first line is handled.
So add call to the first line of your batch file:
call "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Visual Studio 2017\Visual Studio Tools\VC\x64 Native Tools Command Prompt for VS 2017.lnk"
"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.16.27023\bin\Hostx64\x64\cl.exe" D:\temp\ctemplate\2\helloexample.c

Using TF.exe for copy last version in any folder

I currently have a project back up on TFS. What I would like is to recover the files on the server. I tried with this command:
"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\TF.exe" get $/Regie/Dev /recursive %userprofile%\documents\TFS
But the problem is that its copy / update in my file of my local project ... What I would like is to retrieve the project on the server to be able to copy it in any folder.
"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\TF.exe"
Will give you the help you need. You probably want something like this:
"C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\TF.exe" get $/Regie/Dev /version:T /recursive %userprofile%\documents\TFS
You may also want to use /remap. See https://blogs.msdn.microsoft.com/phkelley/2012/05/18/tf-get-remap/

How to create a Simple Build Script for Visual Studio from the Command Line?

I have a lot of Visual Studio Project Solutions in multiple directories (all with the extension .sln) and I want to write a simple batch script that will automatically build all solutions listed in the batch file.
I am able to manually build a solution by starting up the Visual Studio Command Prompt (which is just a command-line instance with the following command executed
"%comspec%" /k "C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
After which I then build the project by calling:
devenv "path\to\solutionFile\projectSolution1.sln" /build Debug
This will build the project (assuming the project does not have errors) and I rinse and repeat for each project I want built.
However when I have the following in a batch file called build.bat:
"%comspec%" /k "C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
echo "Starting Build for all Projects with proposed changes"
echo .
devenv "path\to\solutionFile\projectSolution2.sln" /build Debug
devenv "another\path\to\solutionFile\projectSolution3.sln" /build Debug
devenv "yet\another\path\to\solutionFile\projectSolution4.sln" /build Debug
echo "All builds completed."
pause
The batch script only executes the first line, and waits until I type in exit before executing the others. My understanding of this based on the research I have done on batch files and all the StackOverflow questions is that cmd actually invokes another instance of itself that executes vcvarsall.bat to set up the build environment.
This will not work as typing exit kills that instance with devenv set up and the commands after that cannot execute as devenv is not a recognized command (since the exported path will not exist anymore)
In short, how can this be achieved (passing in the rest of the commands to the cmd instance with devenv defined) in a single batch file? I understand this isn't a robust way (and there are a lot of tools that do this) of invoking builds but I'm just hoping to have one batch script to automate the manual work of individually calling these projects.
Found the solution, as noted by Jimmy, one needs to remove the environment variable %comspec% as that is a shortcut to CMD.exe.
However, just removing "%comspec" /k will cause a CMD instance to open and then exit after a second. I also previously tried the call function which created a separate CMD instance when used with %comspec%
The solution is to add call in front of the first line, and remove %comspec
Here is the final batch file that got things working as intended.
#echo OFF
call "C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
echo "Starting Build for all Projects with proposed changes"
echo .
devenv "path\to\solutionFile\projectSolution2.sln" /build Debug
devenv "another\path\to\solutionFile\projectSolution3.sln" /build Debug
devenv "yet\another\path\to\solutionFile\projectSolution4.sln" /build Debug
echo .
echo "All builds completed."
pause
Note that #echo OFF tells the batch script to not echo out the commands (such as that call command) into the terminal (errors and warnings will still be shown, however)
If this is already in a batch script, then this line:
"%comspec%" /k "C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
Probably should just be "C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" x86
Why? %comspec% is just an environment variable shortcut to cmd.exe, so as you saw, it's launching a new instance of cmd with the /k option specified (which if you run cmd /? says Carries out the command specified by string but remains). You don't care about it remaining, you don't even want a new cmd.exe when you're already running your batch file.

Direct the command to a newly opened separate command prompt using bat file

I have a command prompt shortcut that run a bat file when it's launched. Once it is launched I will issue another command,say my_command, which is added as doskey macro in login.bat
The flow is somewhat like this.
I have a cmd shortcut with target set as:
C:\Windows\System32\cmd.exe /k "D:\\login.bat"
where login.bat sets the environment.
Once the cmd prompt is active I should issue another command, say my_command (which should be run in the currently open cmd prompt)
Inside login.bat I have the following lines
...
doskey my_command=another_login.bat DEBUG 32
I was trying to write a bat file to do the whole thing.
I am not supposed to change login.bat
What I tried is :
start C:\Windows\System32\cmd.exe /k "D:\\login.bat"
my_command
The command my_command gets run on the bat file's command prompt.
How do I make the command my_commnad run on the newly opened command prompt and not in the bat file's cmd prompt?
Try this:
start C:\Windows\System32\cmd.exe /k "D:\\login.bat & devenv"
Answering your edit: bad news.
You cannot run a Doskey macro from a batch file.
Reference: http://ss64.com/nt/doskey.html
Can you just directly run the command you are setting up with doskey?
start C:\Windows\System32\cmd.exe /k "D:\\login.bat & another_login.bat DEBUG 32"
You need to specify the full path to devenv inside the batch file, and that path will depend on your version of Visual Studio.
Try replacing devenv with one of these:
VS 2013:
C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe
VS 2012:
C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\devenv.exe
VS 2010:
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\devenv.exe
Edit in response to your comment
The doskey.exe executable is located in C:\Windows\System32\doskey.exe. Referencing it by full path should work.

How to run Coded UI test file from Visual Studio command prompt using batch file?

#echo off
#setlocal enableextensions
#cd /d "C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE"
start %comspec% /k ""C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat""
MSTest /testcontainer:C:\testdir\test.dll
Code shown above runs vs command prompt and changes directory to "C:\Program Files\Microsoft Visual Studio 10.0\Common7\IDE" where MSTest.exe is. But last line doesn't run in vs command prompt window, opens new window and tries to run in a new opened window. Can anyone help how to run ui test file in opened vs command prompt using batch file?
I run my Coded UI tests with the following batch script:
#echo off
:: Running tests without VS Enterprise is possible if you install the Test Agent package: https://msdn.microsoft.com/en-us/library/dd648127.aspx
set test_runner="C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe"
set test_dlls="C:\Location\Compiled\Tests\Project.CodedUI.Test.dll"
:: If tests is set then only these comma separate test cases are run
:: set tests="Test1,Test2"
set tests=""
if %tests% == "" (
%test_runner% %test_dlls% > CodedUITestResults.txt
) else (
%test_runner% %test_dlls% /tests:%tests%
)
pause
The visual studio number should be replaced per different version
VS2015: 14.0
VS2013: 12.0
Etc

Resources