2016-06-10 22 views
1

我们有一个运行远程XUnit.Net测试的开源项目。其特点是测试主体设计为在远程进程上运行,并将结果序列化回Visual Studio。该项目可以在这里找到。如何禁用所有标记为自定义测试的测试并行执行XUnit.Net中的事实

https://github.com/Weingartner/XUnitRemote

例如测试可能会像

[SampleProcessFact] 
    public void OutOfProcess() 
    { 
     _Output.WriteLine("Process name: " + Process.GetCurrentProcess().ProcessName); 
     Assert.Equal(1,1); 
    } 

SampleProcessFact是宣布如此一个自定义属性。

[AttributeUsage(AttributeTargets.Method)] 
[XunitTestCaseDiscoverer("XUnitRemote.Test.SampleProcessFactDiscoverer", "XUnitRemote.Test")] 
public class SampleProcessFactAttribute : FactAttribute { } 


[AttributeUsage(AttributeTargets.Method)] 
[XunitTestCaseDiscoverer("XUnitRemote.Test.ScheduledSampleProcessFactDiscoverer", "XUnitRemote.Test")] 
public class ScheduledSampleProcessFactAttribute : FactAttribute { } 

[AttributeUsage(AttributeTargets.Method)] 
[XunitTestCaseDiscoverer("XUnitRemote.Test.SampleProcessTheoryDiscoverer", "XUnitRemote.Test")] 
public class SampleProcessTheoryAttribute : TheoryAttribute { } 

查看https://github.com/Weingartner/XUnitRemote/blob/master/XUnitRemote.Test/XUnit.cs#L26的源代码。

但是我们想要一个选项,以便如果我将我的测试用例标记为SampleProcessFact那么测试运行器将只会按顺序运行测试。

我知道我可以标记我所有的测试用例为TestCollection(字符串ID),并应防止连续运行的,但这应该在SampleProcessFact标签如果可能的话进行封装。这应该适用于所有程序集中的所有测试用例。

我的问题只涉及视觉工作室的测试的解雇。远程部分工作正常,但Visual Studio并行调用我们的远程测试用例引擎进行所有测试。

任何想法?

回答

0

我们为解决问题的项目签了字。

https://github.com/Weingartner/XUnitRemote/commit/565e1dfb55f65ff0612afa40bc5d076c69bb739c

一般的解决办法是截获ITestMethod XUnitRemoteTestCaseDiscoverer ::发现内实例,并改写了唯一ID的它的测试集。

public IEnumerable<IXunitTestCase> Discover(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod, IAttributeInfo factAttribute) 
    { 
     if(CollectionId.HasValue) 
      testMethod = WrapTestMethod(testMethod, CollectionId.Value); 

     if (!Process.GetCurrentProcess().ProcessName.Equals(Path.GetFileNameWithoutExtension(_ExePath), StringComparison.OrdinalIgnoreCase)) 
     { 
      yield return new XUnitRemoteTestCase(_DiagnosticMessageSink, discoveryOptions.MethodDisplayOrDefault(), testMethod, _Id, _ExePath); 
     } 
     else 
     { 
      foreach (var testCase in _DefaultTestCaseDiscoverer.Discover(discoveryOptions, testMethod, factAttribute)) 
      { 
       yield return _TestCaseConverter(testCase); 
      } 
     } 
    } 

private ITestMethod WrapTestMethod(ITestMethod testMethod, Guid uniqueId) 
    { 
     var testClass = testMethod.TestClass; 
     var testCollection = testClass.TestCollection; 
     testCollection = new TestCollection 
      (testCollection.TestAssembly, testCollection.CollectionDefinition, testCollection.DisplayName) 
     { 
      UniqueID = uniqueId 
     }; 
     testClass = new TestClass(testCollection, testClass.Class); 
     testMethod = new TestMethod(testClass, testMethod.Method); 
     return testMethod; 
    } 
相关问题