Skip to content

Commit aa39249

Browse files
committed
Implemented TestRunner API
1 parent 95aefed commit aa39249

File tree

6 files changed

+125
-7
lines changed

6 files changed

+125
-7
lines changed

RetailCoder.VBE/App.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Drawing;
44
using System.Globalization;
5+
using System.Windows.Forms;
56
using Microsoft.Vbe.Interop;
67
using Rubberduck.Inspections;
78
using Rubberduck.Parsing;
@@ -59,13 +60,15 @@ private void LoadConfig()
5960
{
6061
_config = _configService.LoadConfiguration();
6162

63+
var currentCulture = RubberduckUI.Culture;
6264
try
6365
{
6466
RubberduckUI.Culture = CultureInfo.GetCultureInfo(_config.UserSettings.LanguageSetting.Code);
6567
}
66-
catch (CultureNotFoundException)
68+
catch (CultureNotFoundException exception)
6769
{
68-
_config.UserSettings.LanguageSetting.Code = "en-US";
70+
MessageBox.Show(exception.Message, "Rubberduck", MessageBoxButtons.OK, MessageBoxIcon.Error);
71+
_config.UserSettings.LanguageSetting.Code = currentCulture.Name;
6972
_configService.SaveConfiguration(_config);
7073
}
7174

RetailCoder.VBE/Rubberduck.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<RegisterForComInterop>true</RegisterForComInterop>
2626
<DocumentationFile>bin\Debug\Rubberduck.XML</DocumentationFile>
2727
<NoWarn>1591</NoWarn>
28+
<PlatformTarget>AnyCPU</PlatformTarget>
2829
</PropertyGroup>
2930
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
3031
<DebugType>full</DebugType>
@@ -550,13 +551,15 @@
550551
<Compile Include="UnitTesting\AssertHandler.cs" />
551552
<Compile Include="UnitTesting\IAssert.cs" />
552553
<Compile Include="UnitTesting\ITestEngine.cs" />
554+
<Compile Include="UnitTesting\ITestRunner.cs" />
553555
<Compile Include="UnitTesting\NewTestMethodCommand.cs" />
554556
<Compile Include="UnitTesting\NewUnitTestModuleCommand.cs" />
555557
<Compile Include="UnitTesting\ProjectTestExtensions.cs" />
556558
<Compile Include="UnitTesting\TestEngine.cs" />
557559
<Compile Include="UnitTesting\TestMethod.cs" />
558560
<Compile Include="UnitTesting\TestOutcome.cs" />
559561
<Compile Include="UnitTesting\TestResult.cs" />
562+
<Compile Include="UnitTesting\TestRunner.cs" />
560563
</ItemGroup>
561564
<ItemGroup>
562565
<COMReference Include="Microsoft.Office.Core">
@@ -566,7 +569,7 @@
566569
<Lcid>0</Lcid>
567570
<WrapperTool>primary</WrapperTool>
568571
<Isolated>False</Isolated>
569-
<EmbedInteropTypes>False</EmbedInteropTypes>
572+
<EmbedInteropTypes>True</EmbedInteropTypes>
570573
<Private>True</Private>
571574
</COMReference>
572575
<COMReference Include="Microsoft.Office.Interop.Excel">

RetailCoder.VBE/UI/UnitTesting/ITestExplorerWindow.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ public interface ITestExplorerWindow : IDockableUserControl
1111
DataGridView GridView { get; }
1212
event EventHandler<DataGridViewCellMouseEventArgs> SortColumn;
1313
BindingList<TestExplorerItem> AllTests { get; set; }
14-
string ClassId { get; }
15-
string Caption { get; }
1614
event EventHandler<SelectedTestEventArgs> OnRunSelectedTestButtonClick;
1715
event EventHandler OnRunLastRunTestsButtonClick;
1816
event EventHandler OnRunNotRunTestsButtonClick;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Runtime.InteropServices;
2+
using Microsoft.Vbe.Interop;
3+
4+
namespace Rubberduck.UnitTesting
5+
{
6+
[ComVisible(true)]
7+
public interface ITestRunner
8+
{
9+
/// <summary>
10+
/// Runs all Rubberduck unit tests in the IDE, optionally outputting results to specified text file.
11+
/// </summary>
12+
/// <param name="vbe"></param>
13+
/// <param name="outputFilePath"></param>
14+
/// <returns>Returns a string containing the test results.</returns>
15+
string RunAllTests(VBE vbe, string outputFilePath = null);
16+
}
17+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Runtime.InteropServices;
6+
using System.Text;
7+
using System.Threading.Tasks;
8+
using Microsoft.Vbe.Interop;
9+
using Rubberduck.Reflection;
10+
11+
namespace Rubberduck.UnitTesting
12+
{
13+
[ComVisible(true)]
14+
[ComDefaultInterface(typeof(ITestRunner))]
15+
[Guid(ClassId)]
16+
[ProgId(ProgId)]
17+
public class TestRunner : ITestRunner
18+
{
19+
private const string ClassId = "C46C141F-30C8-4A8A-B84B-EC04F1CC559B";
20+
private const string ProgId = "Rubberduck.TestRunner";
21+
22+
private readonly TestEngine _engine = new TestEngine();
23+
24+
public TestRunner()
25+
{
26+
_engine.MethodCleanup += TestEngineMethodCleanup;
27+
_engine.MethodInitialize += TestEngineMethodInitialize;
28+
_engine.ModuleCleanup += TestEngine_ModuleCleanup;
29+
_engine.ModuleInitialize += TestEngine_ModuleInitialize;
30+
}
31+
32+
private void TestEngineMethodCleanup(object sender, TestModuleEventArgs e)
33+
{
34+
var module = e.QualifiedModuleName.Component.CodeModule;
35+
module.Parent.RunMethodsWithAttribute<TestCleanupAttribute>();
36+
}
37+
38+
private void TestEngineMethodInitialize(object sender, TestModuleEventArgs e)
39+
{
40+
var module = e.QualifiedModuleName.Component.CodeModule;
41+
module.Parent.RunMethodsWithAttribute<TestInitializeAttribute>();
42+
}
43+
44+
private void TestEngine_ModuleCleanup(object sender, TestModuleEventArgs e)
45+
{
46+
var module = e.QualifiedModuleName.Component.CodeModule;
47+
module.Parent.RunMethodsWithAttribute<ModuleCleanupAttribute>();
48+
}
49+
50+
private void TestEngine_ModuleInitialize(object sender, TestModuleEventArgs e)
51+
{
52+
var module = e.QualifiedModuleName.Component.CodeModule;
53+
module.Parent.RunMethodsWithAttribute<ModuleInitializeAttribute>();
54+
}
55+
56+
private void LoadAllTests(VBE vbe)
57+
{
58+
_engine.AllTests = vbe.VBProjects
59+
.Cast<VBProject>().Where(project => project.Protection != vbext_ProjectProtection.vbext_pp_locked)
60+
.SelectMany(project => project.TestMethods())
61+
.ToDictionary(test => test, test => _engine.AllTests.ContainsKey(test) ? _engine.AllTests[test] : null);
62+
}
63+
64+
public string RunAllTests(VBE vbe, string outputFilePath = null)
65+
{
66+
LoadAllTests(vbe);
67+
_engine.Run(_engine.AllTests.Keys);
68+
69+
var results = OutputToString();
70+
71+
if (outputFilePath != null)
72+
{
73+
OutputToFile(outputFilePath, results);
74+
}
75+
76+
return results;
77+
}
78+
79+
private string OutputToString()
80+
{
81+
var builder = new StringBuilder();
82+
builder.AppendLine("Rubberduck Unit Tests - " + string.Format("{0:G}", DateTime.Now));
83+
84+
foreach (var result in _engine.AllTests)
85+
{
86+
var item = string.Format("{0}\t{1}ms\t{2} {3}", result.Key.QualifiedMemberName, result.Value.Duration, result.Value.Outcome, result.Value.Output);
87+
builder.AppendLine(item);
88+
}
89+
90+
return builder.ToString();
91+
}
92+
93+
private void OutputToFile(string path, string results)
94+
{
95+
File.AppendAllText(path, results);
96+
}
97+
}
98+
}

Rubberduck.UnitTesting/AssertClass.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System.ComponentModel;
2-
using System.Runtime.InteropServices;
1+
using System.Runtime.InteropServices;
32

43
namespace Rubberduck.UnitTesting
54
{

0 commit comments

Comments
 (0)