Git Product home page Git Product logo

xunit.priority's Introduction

Icon

Xunit.Priority NuGet Build status

Provides an ITestCaseOrderer that allows you to control the order of execution of Xunit tests within a class.

Based closely on the code at https://github.com/xunit/samples.xunit/tree/main/TestOrderExamples/TestCaseOrdering

Note that the Xunit folks have stated that they don't believe that well-written unit tests should be dependent on being run in a particular order, which is why this functionality is not available as part of the core package. Nevertheless, there are some testing scenarios which are not strict unit testing and which may require test ordering.

Usage

Add the following attribute to classes for which you want tests run in order:

[TestCaseOrderer(PriorityOrderer.Name, PriorityOrderer.Assembly)]

Then decorate your test methods with the Priority attribute.

[Fact, Priority(-10)]
public void FirstTestToRun() { }

[Fact, Priority(0)]
public void SecondTestToRun() { }

[Fact, Priority(10)]
public void ThirdTestToRunA() { }

[Fact, Priority(10)]
public void ThirdTestToRunB() { }

[Fact]
public void TestsWithNoPriorityRunLast() { }

Priorities are evaluated in numeric order (including 0 and negative numbers). If there are multiple tests with the same priority, those tests will be run in alphabetical order.

By default, tests with no explicit Priority attribute are assigned priority int.MaxValue and will be run last. You can change this by setting a DefaultPriority attribute on your test class.

[DefaultPriority(0)]
public class MyTests
{
    [Fact]
    public void SomeTest() { }

    [Fact]
    public void SomeOtherTest() { }

    [Fact, Priority(10)]
    public void RunMeLast() { }
}

xunit.priority's People

Contributors

aliasadidev avatar asherber avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

xunit.priority's Issues

Can't use it to have one test that runs at the last

It's very easy to have one test that run before everyone else by adding a Priority attribute with everyone else left un-attributed

however, it is impossible to mark one test to run at the end without attributing all others,
the code defaults the Priority to int.MaxValue which leaves no room to append anything later than default, suggest find a mid-value as a default to give flexibility

Thanks

DefaultPriority bug

Hello i have problem about your library. when i use defaultpriority attribute some method who have bigger priority than defaultpriority value its doesnt work/trigger.

 [TestCaseOrderer(PriorityOrderer.Name, PriorityOrderer.Assembly)
     ,DefaultPriority(0)]
 public class ContentControllerTests
 {
     HttpClient client { get; set; }
     static int DefaultFirstItemId { get { return 1; } }
     public ContentControllerTests()
     {
         var server = new TestServer(new WebHostBuilder().UseStartup<VideoSite.Program>());
         client = server.CreateClient();
         Environment.SetEnvironmentVariable("IsInUnitTest", "1");
     }
     [Fact, Priority(-10)]
     public async void Create()
     {
         var data = new ContentViewModel();
         ByteArrayContent img = CreateImageContentForTest.Create();
         var postData = ObjectToMultipartFormDataConverter.Convert(data);
         postData.Add(img, "File", "test.png");

         var res = await client.PostAsync("api/content/create", postData);
         Assert.Equal(HttpStatusCode.OK, res.StatusCode);
     }
     [Fact]
     public async void GetListWithOutParameter()
     {
         var res =await client.GetAsync("api/Content/GetList");
         var values =  JsonConvert.DeserializeObject<List<Content>>(await res.Content.ReadAsStringAsync());

         res.EnsureSuccessStatusCode();

         Assert.Equal(HttpStatusCode.OK,res.StatusCode);
         Assert.Equal(1, values.Count);
     }
     [Fact,Priority(10)]
     public async void Delete()
     {
         var res = await client.GetAsync("api/content/delete/" + DefaultFirstItemId);
         Assert.Equal(HttpStatusCode.OK, res.StatusCode);
     }
 }

resim
xunit version: 2.42
.net version: 7.0.4

Class Priority Control

I have a small automation app scenario where the bulk of my integration tests are all creating an artifact, validating the artifact and then that artifact gets used with a subsequent test. I realize that is horrible, but it is what makes sense for my situation. So, you can consider every class and every test within the classes prioritized.

Unfortunately, Xunit.Priority doesn't seem up to that level of rigid sequencing. It appears to only prioritize the tests within a class, not every test in the run. Know of a simple way of forcing X.P. to run tests in exactly the order they are specified and not allow parallel testing across classes?

I am probably going to ditch X.P. on this project and follow the below article, but it would be real nice if there was a simple XUnit based nuget package out there for accomplishing this. If you are interested in this enhancement request, note that the ideal solution would involve me being able to specify what can/cannot be parallelized.
https://hamidmosalla.com/2018/08/16/xunit-control-the-test-execution-order/

Use in theory tests

Hi, how can I use it in theory tests? I means that use it per InlineData attribute.

async test methods

For async test methods ordering doesn't work? It is required to disable parallelization for that. Is there a way to achieve that?

maybe the default test priority could be 0 instead of maxint

Thanks a lot for your library!

I have a single test with some assertions I want to run after all other tests are finished, but I think the only way to do that now is to apply the DefaultPriority to all test classes.

If the default priority was always 0 I would only have to create a single test with priority 1 for this.

Would that be something you'd accept as change? Or maybe allow the DefaultPriority to be set on assembly level?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.