Skip to content

Code fixer does not convert [FixtureLifeCycle] attribute #4490

@thomhurst

Description

@thomhurst

Description

The NUnit migration code fixer (TUNU0001) does not convert NUnit's [FixtureLifeCycle] attribute to TUnit's equivalent, leaving the unconverted attribute in the code which causes compilation errors.

Reproduction Steps

  1. Create a file with NUnit FixtureLifeCycle attribute:
using NUnit.Framework;

[FixtureLifeCycle(LifeCycle.InstancePerTestCase)]
public class TestClass
{
    private int _counter = 0;

    [Test]
    public void Test1()
    {
        _counter++;
        Assert.That(_counter, Is.EqualTo(1));
    }

    [Test]
    public void Test2()
    {
        _counter++;
        Assert.That(_counter, Is.EqualTo(1));
    }
}
  1. Run the code fixer: dotnet format analyzers --severity info --diagnostics TUNU0001

  2. The attribute remains unconverted:

using System.Threading.Tasks;

[FixtureLifeCycle(LifeCycle.InstancePerTestCase)]
public class TestClass
{
    private int _counter = 0;

    [Test]
    public async Task Test1()
    {
        _counter++;
        await Assert.That(_counter).IsEqualTo(1);
    }

    [Test]
    public async Task Test2()
    {
        _counter++;
        await Assert.That(_counter).IsEqualTo(1);
    }
}
  1. Building produces errors:
error CS0246: The type or namespace name 'FixtureLifeCycle' could not be found
error CS0103: The name 'LifeCycle' does not exist in the current context

Expected Behavior

The code fixer should either:

  1. Convert [FixtureLifeCycle(LifeCycle.InstancePerTestCase)] to TUnit's equivalent (if one exists)
  2. Remove the attribute if TUnit's default behavior matches InstancePerTestCase
  3. Leave a TODO comment indicating manual migration is required

Environment

  • TUnit Version: 1.11.64
  • .NET SDK: 10.0.100-preview.1.25103.12
  • OS: Windows 11

Additional Context

According to NUnit documentation, [FixtureLifeCycle(LifeCycle.InstancePerTestCase)] creates a new instance of the test fixture for each test method. This is useful for ensuring test isolation.


Note: Please add tests when fixing this issue to prevent regression.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions