Skip to content

Commit

Permalink
#2334: Add assembly-level support for BeforeAfterTestAttribute (v2)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradwilson committed Jan 12, 2024
1 parent ba06476 commit fdf75ab
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/xunit.core/Sdk/BeforeAfterTestAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Xunit.Sdk
/// Base attribute which indicates a test method interception (allows code to be run before and
/// after the test is run).
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Method | AttributeTargets.Class, AllowMultiple = true, Inherited = true)]
public abstract class BeforeAfterTestAttribute : Attribute
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ protected virtual List<BeforeAfterTestAttribute> GetBeforeAfterTestAttributes()

return beforeAfterTestCollectionAttributes.Concat(TestClass.GetTypeInfo().GetCustomAttributes(typeof(BeforeAfterTestAttribute)))
.Concat(TestMethod.GetCustomAttributes(typeof(BeforeAfterTestAttribute)))
.Concat(TestClass.GetAssembly().GetCustomAttributes(typeof(BeforeAfterTestAttribute)))
.Cast<BeforeAfterTestAttribute>()
.ToList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ private void AssertMessageSequence(IEnumerable<IMessageSinkMessage> results, str
message => Assert.IsAssignableFrom<ITestStarting>(message),
message => Assert.IsAssignableFrom<ITestClassConstructionStarting>(message),
message => Assert.IsAssignableFrom<ITestClassConstructionFinished>(message),
message => Assert.IsAssignableFrom<IBeforeTestStarting>(message), // from XunitTestCaseRunnerTests.BeforeAfterOnAssembly
message => Assert.IsAssignableFrom<IBeforeTestFinished>(message), // from XunitTestCaseRunnerTests.BeforeAfterOnAssembly
message => Assert.IsAssignableFrom<IAfterTestStarting>(message), // from XunitTestCaseRunnerTests.BeforeAfterOnAssembly
message => Assert.IsAssignableFrom<IAfterTestFinished>(message), // from XunitTestCaseRunnerTests.BeforeAfterOnAssembly
message =>
{
var passed = Assert.IsAssignableFrom<ITestPassed>(message);
Expand Down
9 changes: 9 additions & 0 deletions test/test.xunit.execution/Acceptance/Xunit2AcceptanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@ public void SinglePassingTest()
Assert.Equal(classConstructionFinished.TestCase.DisplayName, classConstructionFinished.Test.DisplayName);
},
message =>
{
// From XunitTestCaseRunnerTests.BeforeAfterOnAssembly
var beforeTestStarting = Assert.IsAssignableFrom<IBeforeTestStarting>(message);
Assert.Equal("BeforeAfterOnAssembly", beforeTestStarting.AttributeName);
},
message => Assert.IsAssignableFrom<IBeforeTestFinished>(message),
message => Assert.IsAssignableFrom<IAfterTestStarting>(message),
message => Assert.IsAssignableFrom<IAfterTestFinished>(message),
message =>
{
var testPassed = Assert.IsAssignableFrom<ITestPassed>(message);
Assert.Equal(testPassed.TestCase.DisplayName, testPassed.Test.DisplayName);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System.Threading;
using System.Linq;
using System.Threading;
using NSubstitute;
using Xunit;
using Xunit.Sdk;

[assembly: XunitTestCaseRunnerTests.BeforeAfterOnAssembly]

public class XunitTestCaseRunnerTests
{
[Fact]
Expand All @@ -16,9 +19,11 @@ public static void BeforeAfterTestAttributesComeFromTestCollectionAndTestClassAn

var runner = new XunitTestCaseRunner(testCase, "Display Name", "Skip Reason", new object[0], new object[0], messageBus, aggregator, tokenSource);

Assert.Collection(runner.BeforeAfterAttributes,
attr => Assert.IsType<BeforeAfterOnCollection>(attr),
Assert.Collection(
runner.BeforeAfterAttributes.OrderBy(a => a.GetType().Name),
attr => Assert.IsType<BeforeAfterOnAssembly>(attr),
attr => Assert.IsType<BeforeAfterOnClass>(attr),
attr => Assert.IsType<BeforeAfterOnCollection>(attr),
attr => Assert.IsType<BeforeAfterOnMethod>(attr)
);
}
Expand All @@ -34,6 +39,7 @@ class ClassUnderTest
public void Passing() { }
}

public class BeforeAfterOnAssembly : BeforeAfterTestAttribute { }
class BeforeAfterOnCollection : BeforeAfterTestAttribute { }
class BeforeAfterOnClass : BeforeAfterTestAttribute { }
class BeforeAfterOnMethod : BeforeAfterTestAttribute { }
Expand Down

0 comments on commit fdf75ab

Please sign in to comment.