Skip to content

Migration: Handle NUnit ExpectedResult attribute property #4167

@thomhurst

Description

@thomhurst

Summary

The NUnit to TUnit migration code fixer should handle the ExpectedResult property in NUnit test attributes and convert it to TUnit's assertion style.

NUnit Pattern

[TestCase(2, 3, ExpectedResult = 5)]
public int Add(int a, int b)
{
    return a + b;
}

[TestCase("hello", ExpectedResult = 5)]
public int GetLength(string input)
{
    return input.Length;
}

Expected TUnit Output

[Test]
[Arguments(2, 3)]
public async Task Add(int a, int b)
{
    await Assert.That(a + b).IsEqualTo(5);
}

[Test]
[Arguments("hello")]
public async Task GetLength(string input)
{
    await Assert.That(input.Length).IsEqualTo(5);
}

Implementation Notes

  1. Detect ExpectedResult = <value> in NUnit attributes ([TestCase], [TestCaseSource], etc.)
  2. Remove ExpectedResult from the migrated [Arguments] attribute
  3. Change method return type from T to async Task
  4. Wrap the return expression with await Assert.That(<expr>).IsEqualTo(<expected>)
  5. Handle various return types (int, string, bool, objects, etc.)

Edge Cases to Consider

  • Methods with multiple return statements
  • Methods with complex return expressions
  • Generic return types
  • Nullable return types
  • ExpectedResult = null

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions