Xunit 1.9.x为用户提供了DynamicSkipExample.cs示例,以帮助他设置动态跳过a [Fact].
事实证明,在执行某些跨平台开发时,这非常有用.这允许在由于底层上下文(OS,文件系统,...)而无法正确运行测试时暂时忽略测试.
但是,此示例已在提交2deeff5中的2.0版本中删除.
如何通过Xunit 2.0的一个扩展点重新实现这样的功能?
注意: xUnit跟踪器中引发了有关此主题的问题.见xunit/xunit#250.
Mat*_*w K 45
[更新: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() { }
}
Run Code Online (Sandbox Code Playgroud)
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 ) };
}
}
}
Run Code Online (Sandbox Code Playgroud)
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();
}
}
Run Code Online (Sandbox Code Playgroud)
您有多种选择可以设置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 ) };
}
}
Run Code Online (Sandbox Code Playgroud)
您还可以在Nugget Manager中搜索SkippableFact,然后您可以使用[SkippableFact]的所有功能