Xunit 1.9.x为用户提供 DynamicSkipExample.cs 示例,以帮助他设置动态跳过[Fact]
。
这已被证明在执行某些跨平台开发时非常有用。这允许在由于底层上下文(OS,文件系统,...)而无法正常运行测试时暂时忽略测试。
但是,此示例已在提交 2deeff5 的版本2.0中被删除。
如何通过Xunit 2.0的一个扩展点重新实现这样的功能?
注意: xUnit跟踪器中引发了有关此主题的问题。请参阅 xunit/xunit#250 。
答案 0 :(得分:43)
[更新: XUnit v2.0(RTM)现已推出,直接支持可跳过的测试。
使用[Fact (Skip = "specific reason")]
]
请注意,XUnit v2.0尚未发货。此示例与您可以在nuget.org上找到的Xunit 2.0 beta5兼容。可能有其他方法可以实现这一点(因为这只是我来的例子)。
1)定义一个将装饰你的测试的属性。
/// <inheritdoc/>
[AttributeUsage( AttributeTargets.Method, AllowMultiple = false )]
[XunitTestCaseDiscoverer( "SkippableTestCaseDiscoverer", "assemblynamehere" )]
public sealed class SkippableTestAttribute : FactAttribute
{
public SkippableTestAttribute() { }
}
2)创建发现者。 (我们遵循了https://github.com/xunit/xunit/blob/2d9ce6fbd75e91a69a0cc83e1bc3d4eab18b2c6c/src/xunit.execution/Sdk/Frameworks/TheoryDiscoverer.cs)
中的代码示例/// <summary>
/// Implementation of <see cref="IXunitTestCaseDiscoverer"/> that supports finding test cases
/// on methods decorated with <see cref="SkippableTestAttribute"/>.
/// </summary>
public class SkippableTestCaseDiscoverer : IXunitTestCaseDiscoverer
{
/// <inheritdoc/>
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Needs to return test case." )]
public IEnumerable<IXunitTestCase> Discover( ITestMethod testMethod, IAttributeInfo factAttribute )
{
// some of this code is from https://github.com/xunit/xunit/blob/2d9ce6fbd75e91a69a0cc83e1bc3d4eab18b2c6c/src/xunit.execution/Sdk/Frameworks/TheoryDiscoverer.cs
if ( factAttribute.GetNamedArgument<string>( "Skip" ) != null )
return new[] { new XunitTestCase( testMethod ) };
var dataAttributes = testMethod.Method.GetCustomAttributes( typeof( DataAttribute ) );
try
{
var results = new List<XunitTestCase>();
foreach ( var dataAttribute in dataAttributes )
{
var discovererAttribute = dataAttribute.GetCustomAttributes( typeof( DataDiscovererAttribute ) ).First();
var discoverer = ExtensibilityPointFactory.GetDataDiscoverer( discovererAttribute );
if ( !discoverer.SupportsDiscoveryEnumeration( dataAttribute, testMethod.Method ) )
return new XunitTestCase[] { new XunitTheoryTestCase( testMethod ) };
// These lines are our "custom dynamic logic" that determines if we should skip the test.
IEnumerable<object[]> data = discoverer.GetData( dataAttribute, testMethod.Method );
if ( data == null )
{
var test = new SkippableTestCase( testMethod );
test.SkipTest( "Test not configured with any " );
return new[] { test };
}
foreach ( var dataRow in data )
{
// Attempt to serialize the test case, since we need a way to uniquely identify a test
// and serialization is the best way to do that. If it's not serializable, this will
// throw and we will fall back to a single theory test case that gets its data
// at runtime.
var testCase = new XunitTestCase( testMethod, dataRow );
SerializationHelper.Serialize( testCase );
results.Add( testCase );
}
}
if ( results.Count == 0 )
results.Add( new LambdaTestCase( testMethod,
() => { throw new InvalidOperationException( String.Format( "No data found for {0}.{1}", testMethod.TestClass.Class.Name, testMethod.Method.Name ) ); } ) );
return results;
}
catch
{
return new XunitTestCase[] { new XunitTheoryTestCase( testMethod ) };
}
}
}
3)创建一个实现IXunitTestCase的类(因为默认基类不允许修改跳过原因)。
// Class is similar to XunitTestCase
[Serializable]
public class SkippableTestCase : TestMethodTestCase, IXunitTestCase
{
[EditorBrowsable(EditorBrowsableState.Never)]
[Obsolete("Called by the de-serializer", error: true)]
public SkippableTestCase() { }
/// <summary>
/// Initializes a new instance of the <see cref="SkippableTestCase"/> class.
/// </summary>
/// <param name="testMethod">The test method this test case belongs to.</param>
/// <param name="testMethodArguments">The arguments for the test method.</param>
public SkippableTestCase(ITestMethod testMethod, object[] testMethodArguments = null)
: base(testMethod, testMethodArguments) { }
/// <inheritdoc />
protected SkippableTestCase(SerializationInfo info, StreamingContext context)
: base(info, context) { }
public void SkipTest( string reason )
{
base.SkipReason = reason;
}
/// <inheritdoc/>
public virtual Task<RunSummary> RunAsync( IMessageBus messageBus, object[] constructorArguments, ExceptionAggregator aggregator, CancellationTokenSource cancellationTokenSource )
{
return new XunitTestCaseRunner( this, DisplayName, SkipReason, constructorArguments, TestMethodArguments, messageBus, aggregator, cancellationTokenSource ).RunAsync();
}
}
您有多种选择可以设置base.SkipReason。在此示例中,创建了一个公共方法。
此示例将跳过具有不返回任何数据行的MemberDataAttribute
的测试。您可以根据自己的条件对其进行修改以返回SkippableTestCase
。例如,这个发现在周日跳过测试。
/// <inheritdoc/>
[System.Diagnostics.CodeAnalysis.SuppressMessage( "Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Needs to return test case." )]
public IEnumerable<IXunitTestCase> Discover( ITestMethod testMethod, IAttributeInfo factAttribute )
{
if ( DateTime.Today.DayOfWeek == DayOfWeek.Sunday )
{
var test = new SkippableTestCase( testMethod );
test.SkipTest( "Test not configured with any " );
return new[] { test };
}
else
{
return new[] { new XunitTestCase( testMethod ) };
}
}
答案 1 :(得分:6)
你也可以在Nugget Manager中搜索SkippableFact,然后你可以使用[SkippableFact]
的所有功能答案 2 :(得分:3)
这最终已在问题 xunit/samples.xunit#8 中修复。
令人惊讶的 @BradWilson 在 samples.xunit 存储库中提供了完整而详细的示例。