Download and Install Visual studio agents from following location:
vs 2012:
https://www.microsoft.com/en-in/download/details.aspx?id=38186
vs 2013:
https://www.microsoft.com/en-in/download/details.aspx?id=40750
vs 2014:
https://www.microsoft.com/en-us/download/details.aspx?id=48152
After downloading extract the folder, you can find the following files:
Open TestAgent and install it.
After installing TestAgent, we can find the following folder and file.
C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\MSTest.exe
Visual studio Solution & code:
Solution Explorer
Layout of MSTest Tool:
MSTestRun.cs File
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace MSTestRun
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void SelectDLL_Click(object sender, EventArgs e)
{
Stream myStream = null;
OpenFileDialog openFileDialog = new OpenFileDialog();
//openFileDialog.InitialDirectory = "c:\\";
openFileDialog.InitialDirectory = @"c:\";
openFileDialog.Filter = "DLL's (*.dll)|*.dll";
openFileDialog.FilterIndex = 2;
openFileDialog.RestoreDirectory = true;
string dllLocation = "";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog.OpenFile()) != null)
{
dllLocation = openFileDialog.FileName;
dllPath.Text = dllLocation;
TestListInAssembly clas = new TestListInAssembly();
List<string> testCases = clas.GetTests(dllLocation);
dataGridView1.DataSource = testCases.Select(x => new { Value = x }).ToList();
dataGridView1.Rows[0].Cells[0].Selected = false;
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
private void ExecuteTest_Click(object sender, EventArgs e)
{
try
{
if (dataGridView1.SelectedCells.Count == 0)
{
MessageBox.Show("No test selected", "Error", MessageBoxButtons.OK);
return;
}
string testName = dataGridView1.SelectedCells[0].Value.ToString();
string batpath = Path.Combine(System.IO.Path.GetTempPath(), "mstestscript.bat");
var command = Environment.GetEnvironmentVariable("ComSpec");
command = @"" + command + @"";
string vsVersion = VsVersionSelected();
string VisualStudioCmdPrompt = string.Format("cd \"C:\\Program Files (x86)\\Microsoft Visual Studio {0}.0\\Common7\\IDE\"", vsVersion);
string vsMstestPath = string.Format("C:\\Program Files (x86)\\Microsoft Visual Studio {0}.0\\Common7\\IDE\\MSTest.exe", vsVersion);
if (!File.Exists(vsMstestPath))
{
MessageBox.Show("Path Not found : " + vsMstestPath, "Path Not found", MessageBoxButtons.OK);
return;
}
CreateBatFile(batpath);
string mstestScript = string.Format("mstest /testcontainer:\"{0}\" /test:{1}", dllPath.Text, testName);
using (StreamWriter sw = new StreamWriter(batpath))
{
sw.WriteLine(VisualStudioCmdPrompt);
sw.WriteLine(mstestScript);
sw.WriteLine("pause");
}
//Executing in Admin mode
ProcessStartInfo info = new ProcessStartInfo(batpath);
info.UseShellExecute = true;
info.Verb = "runas";
Process.Start(info);
//System.Diagnostics.Process.Start(batpath);
}
catch (Exception ex)
{
MessageBox.Show("Unknown Exception, Contact Developer", "Error", MessageBoxButtons.OK);
}
}
public string VsVersionSelected()
{
if (VS2012.Checked)
{
return "11";
}
else if (VS2013.Checked)
{
return "12";
}
else if (VS2015.Checked)
{
return "14";
}
else
{
return "";
}
}
public void CreateBatFile(string batpath)
{
if (File.Exists(batpath))
{
File.Delete(batpath);
}
using (FileStream fs = File.Create(batpath))
{
fs.Close();
}
}
}
}
2. TestListInAssembly.cs
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace MSTestRun
{
public class TestListInAssembly
{
public List<string> GetTests(string dllLocation)
{
List<string> testcasesList = new List<string>();
Assembly assembly = Assembly.LoadFile(dllLocation);
/* This code is used to specfically check the test cases in the Particular Namespace and Class
string Namespace_ClassName = ConfigurationManager.AppSettings.Get("Namespace.ClassName");
Type type = assembly.GetType("Namespace.ClassName");
Type type = assembly.GetType("testsuite.CaligoTest");*/
Type[] types = assembly.GetTypes();
foreach (Type type in types)
{
MethodInfo[] mi = type.GetMethods();
foreach (MethodInfo method in mi)
{
string memeber = method.MemberType.ToString();
object attribute = method.GetCustomAttributes(typeof(TestMethodAttribute)).FirstOrDefault();
if (attribute != null)
{
testcasesList.Add(method.Name);
}
}
}
return testcasesList;
}
}
}