Skip to content

Commit

Permalink
Rudementary MSTest support. TestClass,TestMethod,TestInitialize and T…
Browse files Browse the repository at this point in the history
…estCleanup supported. It
  • Loading branch information
drunkcod committed Aug 29, 2014
1 parent 41ba045 commit f0b386e
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 128 deletions.
1 change: 1 addition & 0 deletions Source/Cone.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
<Compile Include="Runners\ConePadSuiteBuilder.cs" />
<Compile Include="Runners\ConePadTest.cs" />
<Compile Include="Runners\ConeTestMethodSink.cs" />
<Compile Include="Runners\MSTestSuiteBuilder.cs" />
<Compile Include="Runners\MulticastSessionLogger.cs" />
<Compile Include="Runners\NullSessionLogger.cs" />
<Compile Include="Runners\NUnitSuiteBuilder.cs" />
Expand Down
105 changes: 105 additions & 0 deletions Source/Runners/MSTestSuiteBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using Cone.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Cone.Runners
{
public class MSTestSuiteBuilder : ConePadSuiteBuilder
{
class MSTestFixtureDescription : IFixtureDescription
{
private readonly Type type;

public MSTestFixtureDescription(Type type)
{
this.type = type;
}

public IEnumerable<string> Categories
{
get { return new string[0]; }
}

public string SuiteName
{
get { return type.Namespace; }
}

public string SuiteType
{
get { return "TestClass"; }
}

public string TestName
{
get { return type.Name; }
}
}

class MSTestSuite : ConePadSuite
{
class MSTestMethodClassifier : MethodClassifier
{
readonly Type fixtureType;

public MSTestMethodClassifier(Type fixtureType, IConeFixtureMethodSink fixtureSink, IConeTestMethodSink testSink)
: base(fixtureSink, testSink)
{
this.fixtureType = fixtureType;
}

protected override void ClassifyCore(MethodInfo method)
{
if (method.GetParameters().Length > 0)
{
Unintresting(method);
return;
}

var attributes = method.GetCustomAttributes(true);
var attributeNames = attributes.ConvertAll(x => x.GetType().FullName);

if (attributeNames.Contains("Microsoft.VisualStudio.TestTools.UnitTesting.TestInitializeAttribute"))
BeforeEach(method);

if (attributeNames.Contains("Microsoft.VisualStudio.TestTools.UnitTesting.TestCleanupAttribute"))
AfterEach(method);

if (attributeNames.Contains("Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute"))
Test(method, ExpectedTestResult.None);
else Unintresting(method);
}
}

public MSTestSuite(ConeFixture fixture) : base(fixture) { }

protected override IMethodClassifier GetMethodClassifier(IConeFixtureMethodSink fixtureSink, IConeTestMethodSink testSink)
{
return new MSTestMethodClassifier(FixtureType, fixtureSink, testSink);
}
}

public MSTestSuiteBuilder(ObjectProvider objectProvider) : base(objectProvider) { }

public override bool SupportedType(Type type)
{
return type.GetCustomAttributes(true)
.Any(x => x.GetType().FullName == "Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute");
}

public override IFixtureDescription DescriptionOf(Type fixtureType)
{
return new MSTestFixtureDescription(fixtureType);
}

protected override ConePadSuite NewSuite(Type type, IFixtureDescription description)
{
return new MSTestSuite(MakeFixture(type, description.Categories))
{
Name = description.SuiteName + "." + description.TestName
};
}
}
}
2 changes: 1 addition & 1 deletion Source/Runners/NUnitSuiteBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected override void ClassifyCore(MethodInfo method) {
ClassifyParameterized(method, attributes);
return;
}
var attributeNames = attributes.Select(x => x.GetType().FullName).ToArray();
var attributeNames = attributes.ConvertAll(x => x.GetType().FullName);
if(attributeNames.Contains("NUnit.Framework.SetUpAttribute")) {
BeforeEach(method);
}
Expand Down
89 changes: 45 additions & 44 deletions Source/Runners/SimpleConeRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@

namespace Cone.Runners
{
public class SimpleConeRunner
{
class NullSuiteBuilder : IConeSuiteBuilder<ConePadSuite>
{
public bool SupportedType(Type type) { return true; }
public class SimpleConeRunner
{
class NullSuiteBuilder : IConeSuiteBuilder<ConePadSuite>
{
public bool SupportedType(Type type) { return true; }

public ConePadSuite BuildSuite(Type suiteType) {
return null;
}
}
public ConePadSuite BuildSuite(Type suiteType) {
return null;
}
}

public static void RunTests(ISessionLogger logger, IEnumerable<Type> suiteTypes) {
new SimpleConeRunner().RunTests(new TestSession(logger), suiteTypes);
Expand All @@ -31,50 +31,51 @@ public SimpleConeRunner(ObjectProvider objectProvider) {
suiteBuilders = new IConeSuiteBuilder<ConePadSuite>[] {
new ConePadSuiteBuilder(objectProvider),
new NUnitSuiteBuilder(objectProvider),
new MSTestSuiteBuilder(objectProvider),
new NullSuiteBuilder(),
};
}

public int Workers = 1;
public void RunTests(TestSession results, IEnumerable<Assembly> assemblies) {
RunTests(results, assemblies.SelectMany(x => x.GetTypes()));
}
public int Workers = 1;
public void RunTests(TestSession results, IEnumerable<Assembly> assemblies) {
RunTests(results, assemblies.SelectMany(x => x.GetTypes()));
}

public void RunTests(TestSession results, IEnumerable<Type> suiteTypes) {
var toRun = new Queue<ConePadSuite>(suiteTypes
.Choose<Type, ConePadSuite>(TryBuildSuite)
.Flatten(x => x.Subsuites)
.Where(x => results.IncludeSuite(x)));
public void RunTests(TestSession results, IEnumerable<Type> suiteTypes) {
var toRun = new Queue<ConePadSuite>(suiteTypes
.Choose<Type, ConePadSuite>(TryBuildSuite)
.Flatten(x => x.Subsuites)
.Where(x => results.IncludeSuite(x)));

Check.Initialize();
results.RunSession(collectResults => {
ThreadStart runSuite = () => {
for (ConePadSuite suite; ; ) {
lock (toRun) {
if (toRun.Count == 0)
return;
suite = toRun.Dequeue();
}
collectResults(suite);
}
};
var workers = new Thread[Workers - 1];
for (var i = 0; i != workers.Length; ++i) {
var worker = workers[i] = new Thread(runSuite);
worker.Start();
}
runSuite();
workers.ForEach(x => x.Join());
});
results.Report();
}
Check.Initialize();
results.RunSession(collectResults => {
ThreadStart runSuite = () => {
for (ConePadSuite suite; ; ) {
lock (toRun) {
if (toRun.Count == 0)
return;
suite = toRun.Dequeue();
}
collectResults(suite);
}
};
var workers = new Thread[Workers - 1];
for (var i = 0; i != workers.Length; ++i) {
var worker = workers[i] = new Thread(runSuite);
worker.Start();
}
runSuite();
workers.ForEach(x => x.Join());
});
results.Report();
}

bool TryBuildSuite(Type input, out ConePadSuite suite) {
suite = suiteBuilders
.First(x => x.SupportedType(input))
.BuildSuite(input);
.First(x => x.SupportedType(input))
.BuildSuite(input);
return suite != null;
}
}
}
}
107 changes: 24 additions & 83 deletions Specs/Cone.Specs/Runners/MSTestSuiteBuilderSpec.cs
Original file line number Diff line number Diff line change
@@ -1,99 +1,21 @@
using Cone.Core;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;


//mimic MSTest framework attributes
namespace Microsoft.VisualStudio.TestTools.UnitTesting
{
public class TestClassAttribute : Attribute { }

public class TestMethodAttribute : Attribute { }

public class TestInitializeAttribute : Attribute { }

public class TestCleanupAttribute : Attribute { }
}

namespace Cone.Runners
{
class MSTestSuiteBuilder : ConePadSuiteBuilder
{
class MSTestFixtureDescription : IFixtureDescription
{
private readonly Type type;

public MSTestFixtureDescription(Type type) {
this.type = type;
}

public IEnumerable<string> Categories {
get { return new string[0]; }
}

public string SuiteName {
get { return type.Namespace; }
}

public string SuiteType {
get { return "TestClass"; }
}

public string TestName {
get { return type.Name; }
}
}

class MSTestSuite : ConePadSuite
{
class MSTestMethodClassifier : MethodClassifier
{
readonly Type fixtureType;

public MSTestMethodClassifier(Type fixtureType, IConeFixtureMethodSink fixtureSink, IConeTestMethodSink testSink) : base(fixtureSink, testSink) {
this.fixtureType = fixtureType;
}

protected override void ClassifyCore(MethodInfo method)
{
if (method.GetParameters().Length > 0) {
Unintresting(method);
return;
}

var attributes = method.GetCustomAttributes(true);
if (attributes.Any(x => x.GetType().FullName == "Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute"))
Test(method, ExpectedTestResult.None);
else Unintresting(method);
}
}

public MSTestSuite(ConeFixture fixture) : base(fixture) { }

protected override IMethodClassifier GetMethodClassifier(IConeFixtureMethodSink fixtureSink, IConeTestMethodSink testSink)
{
return new MSTestMethodClassifier(FixtureType, fixtureSink, testSink);
}
}

public MSTestSuiteBuilder(ObjectProvider objectProvider) : base(objectProvider) { }

public override bool SupportedType(Type type) {
return type.GetCustomAttributes(true)
.Any(x => x.GetType().FullName == "Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute");
}

public override IFixtureDescription DescriptionOf(Type fixtureType) {
return new MSTestFixtureDescription(fixtureType);
}

protected override ConePadSuite NewSuite(Type type, IFixtureDescription description) {
return new MSTestSuite(MakeFixture(type, description.Categories)) {
Name = description.SuiteName + "." + description.TestName
};
}
}

[Describe(typeof(MSTestSuiteBuilder))]
public class MSTestSuiteBuilderSpec
{
Expand All @@ -104,10 +26,19 @@ class MyMSTestFixture
{
public int Calls;
public int TestCalled;
public int TestInitializeCalled;
public int TestCleanupCalled;

[TestMethod]
public void a_test() { TestCalled = ++Calls; }

[TestInitialize]
public void TestInitialize() { TestInitializeCalled = ++Calls; }

[TestCleanup]
public void TestCleanup() { TestCleanupCalled = ++Calls; }


public void NotATest() { ++Calls; }
}

Expand Down Expand Up @@ -152,9 +83,19 @@ public void CreateFixtureInstance() {
new TestSession(new NullLogger()).RunSession(collectResult => collectResult(MSTestSuite));
}

public void identifies_all_test_methods() {
public void identifies_test_methods() {
Check.That(() => MSTestSuite.TestCount == 1);
}

public void TestInitialize_called_before_test() {
Check.That(
() => MSTestTestClass.TestInitializeCalled > 0,
() => MSTestTestClass.TestInitializeCalled == MSTestTestClass.TestCalled - 1);
}

public void TestCleanup_called_after_test() {
Check.That(() => MSTestTestClass.TestCleanupCalled == MSTestTestClass.TestCalled + 1);
}
}
}
}

0 comments on commit f0b386e

Please sign in to comment.