Run VS unit tests (command line) from Excel

frankhovin

New Member
Joined
Sep 4, 2017
Messages
4
I'm new at Excel programming, so sorry if the question is too basic. However, I've been messing around with it a while without finding a way:

Is it possible to start the VS Developer Command Prompt and a specific command from an Excel macro?
That is, have an Excel macro that starts the VS command prompt, runs the MSTest.exe comand with data from the Excel sheet, and gets the results?

The shortcut for opening the Developer Command Prmpt for VS2015 is
Code:
%comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsDevCmd.bat""
Code:
MSTest.exe /testcontainer:TestProject.dll /test:MainFolder.Subfolder.TestClass.TestMethod
The goal is to have the tests listed in the Excel sheet(s) and a button to run them and get the results back.

NB: Before anyone mentions anything about how Unit Tests are meant to be used, it's worth mentioning that these are Selenium Functional Tests organized and created as VS Unit Tests. So having a GUI and interface like this (Excel) would be a nice solution.
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
You'll need to fire off your processes with the Windows Script Host.

Code:
Set Wsh = CreateObject("Wscript.Shell")
https://msdn.microsoft.com/en-us/library/d5fk67ky(v=vs.84).aspx

This is necessary as opposed to VBA.Shell because you can make this run command wait until it is completed. VBA.Shell will kick off the process and continue with VBA code. A word of warning this doesn't appear to use a current directory so I would make sure to specify the full path to MSTest.exe.

The only way to read the results will be to put them into a .trx file. From what I could tell that looks to be an XML file so I would recommend reading it with the Microsoft Xml Document model.

Code:
Set XmlDoc = CreateObject("MSXML.DOMDocument")
https://msdn.microsoft.com/en-us/library/aa468547.aspx

If you want a temporary place to put that file you can use the TEMP environment variable like so:
Code:
TempDir = Environ$("TEMP")

And of course in the run command with %TEMP%.
 
Upvote 0
Thanks for your help :)

What I've gotten to work so far is this:
Code:
Dim testPath As String
testPath = "..\Source\Workspaces\QA\Opus3Automation\Tests\bin\Debug"
Set Wsh = CreateObject("Wscript.Shell")
Wsh.Run ("%comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsDevCmd.bat"" & d: & cd " & testPath)
This opens the console and navigates to the folder containing the tests.

However, if I try adding more commands - to run the test(s) - like this:
Code:
Wsh.Run ("%comspec% /k ""C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\VsDevCmd.bat"" & d: & cd " & testPath & "MSTest.exe /testcontainer:Tests.dll /test:Tests.Regression_tests.Support.Support_Tickets")
the console doesn't navigate to the testPath folder, and the MSTest.exe command fails because of that.

So, if I stop at the testPath (the working code), the cosole opens and looks like this:

Code:
D:\Source\Workspaces\QA\Automation\Tests\bin\Debug>

From there, I can manually run this command:
Code:
MSTest.exe /testcontainer:Tests.dll /test:Tests.Regression_tests.Support.Support_Tickets
and the tests run fine.

If I add the MSTest.exe command to the VBA script, the console looks like this:
Code:
The filename, directory naeme, or volume label syntax is incorrect.

D:\qa>

So it seems that it doesn't catch all the things I'm trying to do? I can navigate to the correct folder, but if I try to add a line for running the command in the same call it doesn't have time to do the navigation?
 
Upvote 0
And using the whole path to MSTest.exe - C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\MSTest.exe - doesn't make any difference either.

It's clear that I'm not doing this correctly, but I'm struggling with finding the right way.
 
Upvote 0
Of course, I'm apparently both tired and blind. I see now, of course, that when I add more to the end, it's just appended to the "cd" command, so that it'll try to cd to the folder + the command. What I don't know is how to execute separate commans in the shell.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top