Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the test case sequence in xUnit

Tags:

c#

xunit.net

I have written the xUnit test cases in C#. That test class contains so many methods. I need to run the whole test cases in a sequence. How can I set the test case sequence in xUnit?

like image 790
Pankaj Saha Avatar asked Feb 09 '12 11:02

Pankaj Saha


People also ask

How do I set priority in xUnit?

Note that with XUnit. Priority you can define a [DefaultPriority(x)] on class level and a [Priority(y)] on method (Fact) level. And tests with the same priority run in alphabetical order - so it might be sufficient to set a default priority on class level and name the tests alphabetically.

What is the sequence of test execution?

In JUnit, the execution sequence of the same project is: InitTestCase (setUp) TestCase1 (testMethod1) EndTestCase (tearDown)

Does xUnit run tests in parallel?

Running unit tests in parallel is a new feature in xUnit.net version 2. There are two essential motivations that drove us to not only enable parallelization, but also for it to be a feature that's enabled by default: As unit testing has become more prevalent, so too have the number of unit tests.


2 Answers

In xUnit 2.* this can be achieved using the TestCaseOrderer attribute to designate an ordering strategy, which can be used to reference an attribute that is annotated on each test to denote an order.

For example:

Ordering Strategy

[assembly: CollectionBehavior(DisableTestParallelization = true)]   public class PriorityOrderer : ITestCaseOrderer {     public IEnumerable<TTestCase> OrderTestCases<TTestCase>(IEnumerable<TTestCase> testCases) where TTestCase : ITestCase     {         var sortedMethods = new SortedDictionary<int, List<TTestCase>>();          foreach (TTestCase testCase in testCases)         {             int priority = 0;              foreach (IAttributeInfo attr in testCase.TestMethod.Method.GetCustomAttributes((typeof(TestPriorityAttribute).AssemblyQualifiedName)))                 priority = attr.GetNamedArgument<int>("Priority");              GetOrCreate(sortedMethods, priority).Add(testCase);         }          foreach (var list in sortedMethods.Keys.Select(priority => sortedMethods[priority]))         {             list.Sort((x, y) => StringComparer.OrdinalIgnoreCase.Compare(x.TestMethod.Method.Name, y.TestMethod.Method.Name));             foreach (TTestCase testCase in list)                 yield return testCase;         }     }      static TValue GetOrCreate<TKey, TValue>(IDictionary<TKey, TValue> dictionary, TKey key) where TValue : new()     {         TValue result;          if (dictionary.TryGetValue(key, out result)) return result;          result = new TValue();         dictionary[key] = result;          return result;     } } 

Attribute

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] public class TestPriorityAttribute : Attribute {     public TestPriorityAttribute(int priority)     {         Priority = priority;     }      public int Priority { get; private set; } } 

Test Cases

[TestCaseOrderer("FullNameOfOrderStrategyHere", "OrderStrategyAssemblyName")] public class PriorityOrderExamples {     [Fact, TestPriority(5)]     public void Test3()     {         // called third     }      [Fact, TestPriority(0)]     public void Test2()     {       // called second     }      [Fact, TestPriority(-5)]     public void Test1()     {        // called first     }  } 

xUnit 2.* ordering samples here

like image 67
KnowHoper Avatar answered Sep 28 '22 01:09

KnowHoper


Testpriority: at the bottom of this page.

[PrioritizedFixture] public class MyTests {     [Fact, TestPriority(1)]     public void FirstTest()     {         // Test code here is always run first     }     [Fact, TestPriority(2)]     public void SeccondTest()     {         // Test code here is run second     } } 

BTW, I have the same problem right now. And yes, it is not the clean art.. but QA wanted a manual test.. so an automated test with a specific order already is a big leap for them.. (cough) and yes, it is not really unit testing..

like image 25
Andreas Reiff Avatar answered Sep 28 '22 01:09

Andreas Reiff