如何使C#测试按顺序进行?

发布于 2025-02-08 23:16:03 字数 437 浏览 2 评论 0原文

我记得在一个论坛上阅读C#单元/集成/功能测试按字母顺序进行,但事实并非如此。我有1个保存所有测试的文件,并以这样的顺序将它们从上到下放置。

  1. a1_postFirstToreGisterClaimSadmin
  2. b2_createmasterWallettest
  3. c3_sendcoinstoprofile
  4. d4_logintest

,但是当我单击“运行所有测试”时,它以此顺序运行。

那么如何使测试按顺序进行?

I remember reading on a forum that C# Unit/Integration/Functional tests run in alphabetically order but that doesn't seem to be the case. I have 1 file that holds all of my tests and I put them from top to bottom in order like this.

  1. A1_PostFirsttoRegisterClaimsAdmin
  2. B2_CreateMasterWalletTest
  3. C3_SendCoinsToProfile
  4. D4_LoginTest

But when I click "Run All Tests," it runs in this order.
enter image description here

So how do I get the tests to run in order?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深白境迁sunset 2025-02-15 23:16:03

对于Xunit,您必须提供一个测试器。

public class AlphabeticalOrderer : ITestCaseOrderer
{
    public IEnumerable<TTestCase> OrderTestCases<TTestCase>(
        IEnumerable<TTestCase> testCases) where TTestCase : ITestCase =>
        testCases.OrderBy(testCase => testCase.TestMethod.Method.Name);
}

然后,您必须用属性注释测试类才能使用此testorderer。

[TestOrderer("OrdererTypeName", "OrdererAssemblyName")]
public class MyTestClass
{
  [Fact]
  public void A1_PostFirsttoRegisterClaimsAdmin() { }

  [Fact]
  public void B2_CreateMasterWalletTest() { }
}

您可以在 Microsoft Docs

For xUnit you have to provide a TestOrderer.

public class AlphabeticalOrderer : ITestCaseOrderer
{
    public IEnumerable<TTestCase> OrderTestCases<TTestCase>(
        IEnumerable<TTestCase> testCases) where TTestCase : ITestCase =>
        testCases.OrderBy(testCase => testCase.TestMethod.Method.Name);
}

Then you have to annotate your test class with an attribute to use this TestOrderer.

[TestOrderer("OrdererTypeName", "OrdererAssemblyName")]
public class MyTestClass
{
  [Fact]
  public void A1_PostFirsttoRegisterClaimsAdmin() { }

  [Fact]
  public void B2_CreateMasterWalletTest() { }
}

You can find more information about the ordering of unit tests in the Microsoft Docs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文