Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions TUnit.Analyzers.Tests/XUnitMigrationAnalyzerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1487,6 +1487,86 @@ public async Task MyTest()
);
}

[Test]
public async Task Assert_Throws_Sync_Delegate_Stays_Sync()
{
await CodeFixer
.VerifyCodeFixAsync(
"""
{|#0:using System;
using Xunit;

public class MyClass
{
[Fact]
public void MyTest()
{
Assert.Throws<ArgumentException>(() => ThrowException());
}

private void ThrowException() => throw new ArgumentException();
}|}
""",
Verifier.Diagnostic(Rules.XunitMigration).WithLocation(0),
"""
using System;

public class MyClass
{
[Test]
public void MyTest()
{
Assert.Throws<ArgumentException>(() => ThrowException());
}

private void ThrowException() => throw new ArgumentException();
}
""",
ConfigureXUnitTest
);
}

[Test]
public async Task Assert_ThrowsAsync_Async_Delegate_Stays_Async()
{
await CodeFixer
.VerifyCodeFixAsync(
"""
{|#0:using System;
using System.Threading.Tasks;
using Xunit;

public class MyClass
{
[Fact]
public async Task MyTest()
{
await Assert.ThrowsAsync<ArgumentException>(() => ThrowExceptionAsync());
}

private Task ThrowExceptionAsync() => Task.FromException(new ArgumentException());
}|}
""",
Verifier.Diagnostic(Rules.XunitMigration).WithLocation(0),
"""
using System;
using System.Threading.Tasks;

public class MyClass
{
[Test]
public async Task MyTest()
{
await Assert.ThrowsAsync<ArgumentException>(() => ThrowExceptionAsync());
}

private Task ThrowExceptionAsync() => Task.FromException(new ArgumentException());
}
""",
ConfigureXUnitTest
);
}

[Test]
public async Task Assert_Throws_With_Message_Contains_Can_Be_Converted()
{
Expand Down
11 changes: 6 additions & 5 deletions docs/docs/migration/xunit.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Migrating from xUnit to TUnit can improve test execution speed. Check the [bench
| `IAsyncLifetime` | `[Before(Test)]` / `[After(Test)]` |
| `ITestOutputHelper` | `TestContext` parameter |
| `Assert.Equal(expected, actual)` | `await Assert.That(actual).IsEqualTo(expected)` |
| `Assert.Throws<T>(() => ...)` | `await Assert.ThrowsAsync<T>(() => ...)` |
| `Assert.Throws<T>(() => ...)` | `Assert.Throws<T>(() => ...)` |

## Automated Migration with Code Fixers

Expand All @@ -35,7 +35,7 @@ TUnit includes Roslyn analyzers and code fixers that automate most of the migrat
- `[Trait("key", "value")]` → `[Property("key", "value")]`
- `Assert.Equal(expected, actual)` → `await Assert.That(actual).IsEqualTo(expected)`
- `Assert.True(condition)` → `await Assert.That(condition).IsTrue()`
- `Assert.Throws<T>(...)` → `await Assert.ThrowsAsync<T>(...)`
- `Assert.Throws<T>(...)` → `Assert.Throws<T>(...)`
- `Assert.Contains(item, collection)` → `await Assert.That(collection).Contains(item)`
- Test methods converted to `async Task` with `await` on assertions

Expand Down Expand Up @@ -1022,9 +1022,9 @@ public async Task Async_Exception_Assertions()
[Test]
public async Task Exception_Assertions()
{
await Assert.ThrowsAsync<ArgumentException>(() => ThrowsException());
Assert.Throws<ArgumentException>(() => ThrowsException());

var ex = await Assert.ThrowsAsync<ArgumentException>(() => ThrowsException());
var ex = Assert.Throws<ArgumentException>(() => ThrowsException());
await Assert.That(ex.ParamName).IsEqualTo("paramName");
}

Expand All @@ -1036,7 +1036,8 @@ public async Task Async_Exception_Assertions()
```

**Key Changes:**
- Both sync and async use `Assert.ThrowsAsync` in TUnit
- Sync exception assertions use `Assert.Throws`
- Async exception assertions use `Assert.ThrowsAsync`
- Returned exception can be further asserted on

### Complete Example: Real-World Test Class
Expand Down
Loading