I know you generally should not depend on order for your unit tests, but in xunit is it possible to make your tests run in a certain order?
-
Does this answer your question? [How to set the test case sequence in xUnit](https://stackoverflow.com/questions/9210281/how-to-set-the-test-case-sequence-in-xunit) – Matt Jan 28 '21 at 14:03
3 Answers
There is a sample in our Samples project named PrioritizedFixtureSample which allows you to control the ordering of tests.
See the samples in our latest release: https://xunit.net/#releases

- 35,463
- 9
- 80
- 98

- 67,914
- 9
- 74
- 83
-
12In short, the second parameter to the `[Fact]` method attribute is called `TestPriority` which in itself takes an integer (signed) representing the order of the tests. Exhibit A: `[Fact, TestPriority(5)]`. Just in case you didn't want to download and load up the example project... – Kieran Senior Jan 23 '12 at 15:42
-
2`Fact` doesn't have seem to have `TestPriority` property anymore. – Fitzchak Yitzchaki Apr 19 '12 at 16:40
-
I don't think it was ever in xUnit itself, look up the sample Brad mentioned, the code for the attribute is in there. – Jun 19 '12 at 07:48
-
2I also created a version that executes modules alphabetically. And uses PriorityFixtures as well. So you can determine the exact ordering - if needed. (Class order through naming, like TestCase01, TestCase02, ...) http://www.andreas-reiff.de/2012/06/xunit-with-alphabetically-sorted-classes-and-proper-display-list-matching-execution-order/ , for a similar question and similar answers look at http://stackoverflow.com/questions/9210281/how-to-set-the-test-case-sequence-in-xunit/11211507#comment20154201_11211507 . – Andreas Reiff Jan 22 '13 at 21:20
-
6`[TestPriority]` **IS NOT** part of `xUnit`. See the links provided by @AndreasReiff for implementation examples. – Chris Marisic Sep 08 '14 at 20:06
No, I don't believe so, but then unit tests by definition should be independent so order shouldn't matter. Where you do have a natural dependency you can't separate I'd suggest you combine the tests into one unit with multiple asserts.

- 74,572
- 17
- 113
- 180
xUnit.net does not provide a way to order tests.
Some other frameworks do, however. For example, in mbUnit, you can attach an Order property to your test attributes. Many TDD purists feel that this is abusive, and any test that requires an order should be merged into a separate unit test, but many people find it useful to be able to order tests in certain circumstances.

- 554,122
- 78
- 1,158
- 1,373