Commit 3f244da2 authored by Michael Herndon's avatar Michael Herndon

WIP: AB#1 move back to inherit from fact attribute

- Inheriting from the FactAttribute is a must for the xunit runners execept for the console.
- Add a FactDiscoverer that creates a MettleTestCase for the FactAttribute.
- Replace Xunit.Sdk.FactDiscoverer => Mettle.Xunit.Sdk.FactDiscoverer.
- Fix merging issue with the solution.
parent 8a11921a
......@@ -55,15 +55,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Api", "Api", "{62C1F2BF-BB1
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NerdyMishka.Api.Users", "src\Api\Users\NerdyMishka.Api.Users.csproj", "{CBFD856A-D652-4726-B3D5-C285265E9D2D}"
EndProject
<<<<<<< HEAD
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Testing", "Testing", "{0C17BD1C-2336-4E6E-8088-B7D8BF97597E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mettle.Xunit.Tests", "test\Testing\Mettle.Xunit.Tests\Mettle.Xunit.Tests.csproj", "{4A75359E-A1DF-4679-909A-5CE2BC30AE10}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Mettle.Xunit", "src\Testing\Mettle.Xunit\Mettle.Xunit.csproj", "{7837AD7B-6DB1-4B12-B13F-4221C897E717}"
EndProject
=======
>>>>>>> parent of 85893e8... WIP: AB#1 finish up importin XUnit runners
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
......@@ -130,7 +127,6 @@ Global
{CBFD856A-D652-4726-B3D5-C285265E9D2D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CBFD856A-D652-4726-B3D5-C285265E9D2D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CBFD856A-D652-4726-B3D5-C285265E9D2D}.Release|Any CPU.Build.0 = Release|Any CPU
<<<<<<< HEAD
{4A75359E-A1DF-4679-909A-5CE2BC30AE10}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4A75359E-A1DF-4679-909A-5CE2BC30AE10}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4A75359E-A1DF-4679-909A-5CE2BC30AE10}.Release|Any CPU.ActiveCfg = Release|Any CPU
......@@ -139,8 +135,6 @@ Global
{7837AD7B-6DB1-4B12-B13F-4221C897E717}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7837AD7B-6DB1-4B12-B13F-4221C897E717}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7837AD7B-6DB1-4B12-B13F-4221C897E717}.Release|Any CPU.Build.0 = Release|Any CPU
=======
>>>>>>> parent of 85893e8... WIP: AB#1 finish up importin XUnit runners
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......
......@@ -5,9 +5,9 @@ using Xunit.Sdk;
namespace Mettle
{
[XunitTestCaseDiscoverer("Mettle.Xunit.Sdk.TestCaseDiscoverer", "Mettle.Xunit")]
public class IntegrationAttribute : TestCaseAttribute
public class IntegrationTestAttribute : TestCaseAttribute
{
public IntegrationAttribute()
public IntegrationTestAttribute()
{
this.Tags = this.Tags ?? "integration";
}
......
using System.Collections.Generic;
using System.Linq;
using Xunit.Abstractions;
using Xunit.Sdk;
namespace Mettle.Xunit.Sdk
{
/// <summary>
/// Implementation of <see cref="IXunitTestCaseDiscoverer"/> that supports finding test cases
/// on methods decorated with <see cref="FactAttribute"/>.
/// </summary>
public class FactDiscoverer : IXunitTestCaseDiscoverer
{
/// <summary>
/// Initializes a new instance of the <see cref="FactDiscoverer"/> class.
/// </summary>
/// <param name="diagnosticMessageSink">The message sink used to send diagnostic messages</param>
public FactDiscoverer(IMessageSink diagnosticMessageSink)
{
DiagnosticMessageSink = diagnosticMessageSink;
}
/// <summary>
/// Gets the message sink used to report <see cref="IDiagnosticMessage"/> messages.
/// </summary>
protected IMessageSink DiagnosticMessageSink { get; }
/// <summary>
/// Creates a single <see cref="XunitTestCase"/> for the given test method.
/// </summary>
/// <param name="discoveryOptions">The discovery options to be used.</param>
/// <param name="testMethod">The test method.</param>
/// <param name="factAttribute">The attribute that decorates the test method.</param>
/// <returns></returns>
protected virtual IXunitTestCase CreateTestCase(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod, IAttributeInfo factAttribute)
=> new MettleTestCase(DiagnosticMessageSink, discoveryOptions.MethodDisplayOrDefault(), discoveryOptions.MethodDisplayOptionsOrDefault(), testMethod, null);
/// <summary>
/// Discover test cases from a test method. By default, if the method is generic, or
/// it contains arguments, returns a single <see cref="ExecutionErrorTestCase"/>;
/// otherwise, it returns the result of calling <see cref="CreateTestCase"/>.
/// </summary>
/// <param name="discoveryOptions">The discovery options to be used.</param>
/// <param name="testMethod">The test method the test cases belong to.</param>
/// <param name="factAttribute">The fact attribute attached to the test method.</param>
/// <returns>Returns zero or more test cases represented by the test method.</returns>
public virtual IEnumerable<IXunitTestCase> Discover(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod, IAttributeInfo factAttribute)
{
IXunitTestCase testCase;
if (testMethod.Method.GetParameters().Any())
testCase = new ExecutionErrorTestCase(DiagnosticMessageSink, discoveryOptions.MethodDisplayOrDefault(), discoveryOptions.MethodDisplayOptionsOrDefault(), testMethod, "[Fact] methods are not allowed to have parameters. Did you mean to use [Theory]?");
else if (testMethod.Method.IsGenericMethodDefinition)
testCase = new ExecutionErrorTestCase(DiagnosticMessageSink, discoveryOptions.MethodDisplayOrDefault(), discoveryOptions.MethodDisplayOptionsOrDefault(), testMethod, "[Fact] methods are not allowed to be generic.");
else
testCase = CreateTestCase(discoveryOptions, testMethod, factAttribute);
return new[] { testCase };
}
}
}
......@@ -138,8 +138,6 @@ namespace Mettle.Xunit.Sdk
protected override void Initialize()
{
base.Initialize();
var serviceProviderFactoryAttribute = TestMethod.Method
.GetCustomAttributes(typeof(ServiceProviderFactoryAttribute))
.SingleOrDefault();
......@@ -156,37 +154,43 @@ namespace Mettle.Xunit.Sdk
}
}
var factAttribute = TestMethod.Method.GetCustomAttributes(typeof(FactAttribute)).FirstOrDefault();
bool isTestCase = true;
var factAttribute =TestMethod.Method.GetCustomAttributes(typeof(TestCaseAttribute)).FirstOrDefault();
if(factAttribute == null)
factAttribute =TestMethod.Method.GetCustomAttributes(typeof(TestCaseAttribute)).FirstOrDefault();
{
factAttribute = TestMethod.Method.GetCustomAttributes(typeof(FactAttribute)).FirstOrDefault();
isTestCase = false;
}
var baseDisplayName = factAttribute.GetNamedArgument<string>("DisplayName") ?? BaseDisplayName;
DisplayName = GetDisplayName(factAttribute, baseDisplayName);
SkipReason = GetSkipReason(factAttribute);
Timeout = GetTimeout(factAttribute);
string id = factAttribute.GetNamedArgument<string>("Id");
string ticket= factAttribute.GetNamedArgument<string>("Ticket");
string tags = factAttribute.GetNamedArgument<string>("Tags");
if(isTestCase)
{
string id = factAttribute.GetNamedArgument<string>("Id");
string ticket= factAttribute.GetNamedArgument<string>("Ticket");
string tags = factAttribute.GetNamedArgument<string>("Tags");
if(!string.IsNullOrWhiteSpace(id))
Traits.Add("id", id);
if(!string.IsNullOrWhiteSpace(id))
Traits.Add("id", id);
if(!string.IsNullOrWhiteSpace(ticket))
Traits.Add("ticket", ticket);
if(!string.IsNullOrWhiteSpace(ticket))
Traits.Add("ticket", ticket);
if(!string.IsNullOrWhiteSpace(tags))
{
var set = tags.Split(';').Select(o => o.Trim()).ToArray();
foreach(var tag in set)
if(!string.IsNullOrWhiteSpace(tags))
{
Traits.Add("Category", tag);
Traits.Add("tag", tag);
var set = tags.Split(';').Select(o => o.Trim()).ToArray();
foreach(var tag in set)
{
Traits.Add("Category", tag);
Traits.Add("tag", tag);
}
}
}
}
foreach (var traitAttribute in GetTraitAttributesData(TestMethod))
{
......
......@@ -145,11 +145,11 @@ namespace Mettle.Xunit.Sdk
// support backwards compatability of Fact/Theory
// however, convert the test case to MettleTestCase.
if(discovererType == typeof(FactDiscoverer))
discovererType = typeof(TestCaseDiscoverer);
if(discovererType == typeof(Xunit.Sdk.FactDiscoverer))
discovererType = typeof(Mettle.Xunit.Sdk.FactDiscoverer);
if(discovererType == typeof(TheoryDiscoverer))
discovererType = typeof(MettleTheoryDiscoverer);
//if(discovererType == typeof(TheoryDiscoverer))
// discovererType = typeof(MettleTheoryDiscoverer);
var discoverer = GetDiscoverer(discovererType);
if (discoverer == null)
......@@ -209,6 +209,9 @@ namespace Mettle.Xunit.Sdk
$":{testCase.TestMethod.TestClass.TestCollection.UniqueID.ToString("N")}";
}
return base.Serialize(testCase);
}
}
......
......@@ -7,7 +7,7 @@ namespace Mettle
[System.AttributeUsage(System.AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
public abstract class TestCaseAttribute : System.Attribute
public abstract class TestCaseAttribute : FactAttribute
{
......@@ -39,19 +39,5 @@ namespace Mettle
/// </summary>
/// <value></value>
public string Tags { get; set; }
/// <summary>
/// Gets or sets the timeout for the test.
/// </summary>
/// <value></value>
public int Timeout { get; set; }
/// <summary>
/// Gets or sets the display name
/// </summary>
/// <value></value>
public string DisplayName { get; set; }
}
}
\ No newline at end of file
......@@ -6,9 +6,9 @@ using Xunit.Sdk;
namespace Mettle
{
[XunitTestCaseDiscoverer("Mettle.Xunit.Sdk.TestCaseDiscoverer", "Mettle.Xunit")]
public class UnitAttribute : TestCaseAttribute
public class UnitTestAttribute : TestCaseAttribute
{
public UnitAttribute()
public UnitTestAttribute()
{
this.Tags = this.Tags ?? "unit";
}
......
......@@ -7,7 +7,7 @@ namespace Mettle.Xunit.Tests
public class UnitTest1
{
[Unit]
[UnitTest]
public void Test1()
{
Console.Write("Test");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment