Skip to content

feat: support data passing between [DependsOn] tests #4763

@thomhurst

Description

@thomhurst

Problem

When using [DependsOn] for workflow/sequential tests, there's no way to pass data from one test to the next. The current workaround is fragile static fields:

private static int _orderId; // fragile shared state

[Test]
public async Task Step1_Place_Order()
{
    var order = await CreateOrder();
    _orderId = order.Id; // side effect
}

[Test, DependsOn(nameof(Step1_Place_Order))]
public async Task Step2_Process_Payment()
{
    // Uses _orderId - but if Step1 failed, _orderId is 0
    await PayOrder(_orderId);
}

Problems with static fields:

  • No type safety between producer and consumer
  • Default values if predecessor fails (silent bugs)
  • Not obvious which test produces which data
  • Doesn't work well with parallel test classes

Proposed API

// Option A: Return value + parameter injection
[Test]
public async Task<int> Step1_Place_Order()
{
    var order = await CreateOrder();
    return order.Id; // explicit output
}

[Test, DependsOn(nameof(Step1_Place_Order))]
public async Task Step2_Process_Payment(
    [DependsOnResult(nameof(Step1_Place_Order))] int orderId)
{
    await PayOrder(orderId); // type-safe, injected
}

// Option B: TestContext-based state bag (simpler to implement)
[Test]
public async Task Step1_Place_Order(TestContext context)
{
    var order = await CreateOrder();
    context.SetResult(order.Id);
}

[Test, DependsOn(nameof(Step1_Place_Order))]
public async Task Step2_Process_Payment(TestContext context)
{
    var orderId = context.GetDependencyResult<int>(nameof(Step1_Place_Order));
}

Use Cases

  • E2E workflow tests (create → process → verify)
  • Setup chains where each step produces an ID/token/resource needed by the next
  • Multi-step API interaction tests

Context

Discovered while building the CloudShop Aspire + TUnit example (#4761). The OrderWorkflowTests class needed a 4-step dependency chain sharing an order ID.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions