Language: C#
Statlight Test Runner Plugin (C#)
using System.IO; using System.Reflection; using System.Timers; using System.Windows.Forms; using EnvDTE; using Timer = System.Timers.Timer; namespace SilverlightTestRunner { public class TestRunner { private readonly DTE _applicationObject; private Timer _timer; private string _testName; private CodeFunction _selectedFunction; private CodeClass _selectedClass; public TestRunner(DTE applicationObject) { _applicationObject = applicationObject; StartBuild(); } private void StartBuild() { _testName = GetTestName(); if (string.IsNullOrEmpty(_testName)) { MessageBox.Show("Cursor is not in a valid Silverlight TestMethod or TestClass, move cursor and try again."); return; } _timer = new Timer { Interval = 1000, Enabled = true }; _timer.Elapsed += TimerLapsedEvent; _timer.Start(); _applicationObject.ExecuteCommand("Build.BuildSelection"); } private void TimerLapsedEvent(object e, ElapsedEventArgs args) { if (_applicationObject.DTE.Solution.SolutionBuild.BuildState == vsBuildState.vsBuildStateDone) { _timer.Stop(); RunSilverlightTest(); } } private void RunSilverlightTest() { Project project; if (_selectedFunction != null) { project = _selectedFunction.ProjectItem.ContainingProject; } else { project = _selectedClass.ProjectItem.ContainingProject; } var config = project.ConfigurationManager.ActiveConfiguration; var xapPath = Path.GetDirectoryName(project.FullName) + "\\" + config.Properties.Item("OutputPath").Value + project.Properties.Item("AssemblyName").Value + ".xap"; var command = @"""C:\Program Files (x86)\Statlight\statlight.exe"" -x=" + xapPath + " -t=" + _testName; //TODO: Run test in continuous mode, put testname in process title, look for process with testname already running and only build and switch focus to process if it exists RunStatlight(command); } private void RunStatlight(string command) { string tempFilename = Path.ChangeExtension(Path.GetTempFileName(), ".bat"); using (StreamWriter writer = new StreamWriter(tempFilename)) { writer.WriteLine("@ECHO OFF"); writer.WriteLine(command); writer.WriteLine("PAUSE"); } var process = System.Diagnostics.Process.Start(tempFilename); process.WaitForExit(); File.Delete(tempFilename); } private string GetTestName() { var testName = ""; var selection = (TextSelection)_applicationObject.ActiveDocument.Selection; var selectedClass = selection.ActivePoint.CodeElement[vsCMElement.vsCMElementClass]; var selectedFunction = selection.ActivePoint.CodeElement[vsCMElement.vsCMElementFunction]; if (selectedFunction != null) { _selectedFunction = (CodeFunction)selectedFunction; var isTestMethod = false; foreach (var attribute in _selectedFunction.Attributes) { if (((CodeElement)attribute).Name == "TestMethod") { isTestMethod = true; break; } } if (isTestMethod) { testName = _selectedFunction.Name; } } else if (selectedClass != null) { _selectedClass = (CodeClass)selectedFunction; var isTestClass = false; foreach (var attribute in _selectedClass.Attributes) { if (((CodeElement)attribute).Name == "TestMethod") { isTestClass = true; break; } } if (isTestClass) { testName = _selectedClass.Name; } } return testName; } } }
Tags:
Report Abuse
Subscribe
Discuss
What's new
What is it
New Snippet
Recent Snippets
My Snippets
Web Code
Search

