You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于NUnit TestCase中ApprovalTests.Net指定单独审批文件的问询

How to Get Separate Approval Files for Each NUnit TestCase in ApprovalTests.Net

Absolutely! This is a super common need, and ApprovalTests.Net has straightforward ways to make each TestCase use its own approval file. Let’s break down the solutions based on your scenario:

1. Automatic Naming for Simple TestCase Parameters

If your TestCases use primitive types (strings, numbers, etc.), ApprovalTests.Net will automatically append the parameter values to the approval file name by default. For example:

using ApprovalTests.NUnit;
using NUnit.Framework;

[TestFixture]
public class CustomerReportTests
{
    [Test]
    [TestCase("CustomerA")]
    [TestCase("CustomerB")]
    public void GenerateCustomerReport_ValidId_ReturnsCorrectReport(string customerId)
    {
        var reportContent = CreateCustomerReport(customerId);
        Approvals.Verify(reportContent);
    }

    private string CreateCustomerReport(string customerId)
    {
        // Your report generation logic here
        return $"Report for {customerId}: ...";
    }
}

This will generate two distinct approval files:

  • GenerateCustomerReport_ValidId_ReturnsCorrectReport.CustomerA.approved.txt
  • GenerateCustomerReport_ValidId_ReturnsCorrectReport.CustomerB.approved.txt

Just make sure you’ve installed the ApprovalTests.NUnit NuGet package—this integration handles TestCase parameter detection out of the box.

2. Handling Complex TestCase Parameters

If you’re using custom objects as TestCase parameters, you have two reliable options to get unique file names:

Option A: Override ToString() on Your Custom Type

ApprovalTests uses the ToString() output of parameters to name files. Override it to return a unique, readable string for each instance:

public class Customer
{
    public string Id { get; set; }
    public string FullName { get; set; }

    public override string ToString()
    {
        // Return a unique identifier for the customer (avoid spaces/special chars)
        return $"{Id}_{FullName.Replace(" ", "_")}";
    }
}

[Test]
[TestCaseSource(nameof(TestCustomers))]
public void GenerateCustomerReport_ValidCustomer_ReturnsCorrectReport(Customer customer)
{
    var reportContent = CreateCustomerReport(customer);
    Approvals.Verify(reportContent);
}

private static IEnumerable<Customer> TestCustomers()
{
    yield return new Customer { Id = "C001", FullName = "Alice Smith" };
    yield return new Customer { Id = "C002", FullName = "Bob Johnson" };
}

This will create files like GenerateCustomerReport_ValidCustomer_ReturnsCorrectReport.C001_Alice_Smith.approved.txt.

Option B: Explicitly Define a Scenario Name

Use ApprovalResults.ForScenario() to manually specify unique identifiers for each test case, giving you full control over the file name suffix without modifying the original class:

[Test]
[TestCaseSource(nameof(TestCustomers))]
public void GenerateCustomerReport_ValidCustomer_ReturnsCorrectReport(Customer customer)
{
    var reportContent = CreateCustomerReport(customer);
    // Explicitly set the scenario using customer properties
    ApprovalResults.ForScenario(customer.Id, customer.FullName);
    Approvals.Verify(reportContent);
}

This produces the same clear file naming as the ToString() approach, but lets you pick exactly which properties to use.

3. Advanced: Custom Approval Namer (For Edge Cases)

If you need even more control over file naming (like using different conventions for different test classes), you can implement a custom IApprovalNamer and register it. This is rare for most use cases, but here’s a quick example:

public class CustomTestCaseNamer : UnitTestFrameworkNamer
{
    protected override string GetTestName()
    {
        var testContext = TestContext.CurrentContext;
        var baseName = base.GetTestName();
        var parameters = string.Join("_", testContext.Test.Arguments.Select(a => a.ToString()));
        return $"{baseName}.{parameters}";
    }
}

// Register the custom namer in your test setup
[SetUp]
public void Setup()
{
    ApprovalTests.Namers.ApprovalResults.UseNamer(() => new CustomTestCaseNamer());
}

Quick Troubleshooting Tip

If automatic naming isn’t working, double-check that:

  • You’re using ApprovalTests.NUnit (not just the core ApprovalTests package)
  • Your TestCase parameters are being correctly passed to the test method
  • You haven’t overridden the default namer elsewhere in your codebase

内容的提问来源于stack exchange,提问作者phoenix

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 10:42:47