Skip to content

TUXU0001 code fixer does not remove all 'using Xunit;' statements #4482

@thomhurst

Description

@thomhurst

Description

When running the TUXU0001 code fixer for xUnit migration, the code fixer adds TUnit usings but does not consistently remove the using Xunit; statements, leaving them in some files.

Steps to Reproduce

  1. Create a test file with xUnit:
using System.Threading.Tasks;

namespace Moq.Tests.Async
{
    using System.Threading.Tasks;
    using Moq.Async;
    using Xunit;

    public class AwaitableFixture
    {
        [Fact]
        public void TryGetResultRecursive_is_recursive()
        {
            const int expectedResult = 42;
            var obj = Task.FromResult(Task.FromResult(expectedResult));
            var result = Awaitable.TryGetResultRecursive(obj);
            Assert.Equal(expectedResult, result);
        }
    }
}
  1. Install TUnit package alongside xUnit
  2. Run the code fixer: dotnet format analyzers --severity warn --diagnostics TUXU0001

Expected Behavior

The using Xunit; statement should be removed and replaced with appropriate TUnit usings:

using System.Threading.Tasks;

namespace Moq.Tests.Async
{
    using System.Threading.Tasks;
    using Moq.Async;
    // using Xunit; should be removed

    public class AwaitableFixture
    {
        [Test]
        public async Task TryGetResultRecursive_is_recursive()
        {
            // ...
        }
    }
}

Actual Behavior

The code fixer converts the test attributes and assertions but leaves the using Xunit; statement in place:

using System.Threading.Tasks;

namespace Moq.Tests.Async
{
    using System.Threading.Tasks;
    using Moq.Async;
    using Xunit;  // <-- Still present!

    public class AwaitableFixture
    {
        [Test]  // Converted
        public async Task TryGetResultRecursive_is_recursive()
        {
            // Converted assertions
        }
    }
}

This causes:

  • CS0246: The type or namespace name 'Xunit' could not be found after removing xUnit package

Environment

  • TUnit Version: 1.11.64
  • .NET SDK: 10.0.102
  • OS: Windows

Observation

This may be related to the using statement being inside a namespace block rather than at the file level.

Tests to Add

Please add tests for:

  1. using Xunit; at file level
  2. using Xunit; inside namespace block
  3. Multiple using statements on same line
  4. Using alias like using X = Xunit;

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