Skip to content

Commit 1f6ac15

Browse files
committed
feat: Add custom admonitions for performance and migration tips
1 parent 7212b94 commit 1f6ac15

File tree

18 files changed

+1390
-170
lines changed

18 files changed

+1390
-170
lines changed

docs/docs/advanced/performance-best-practices.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
This guide provides recommendations for optimizing test performance and ensuring your TUnit test suite runs efficiently.
44

5+
:::performance
6+
Want to see how fast TUnit can be? Check out the [performance benchmarks](/docs/benchmarks) showing real-world speed comparisons, or use the [benchmark calculator](/docs/benchmarks/calculator) to estimate potential time savings for your specific test suite.
7+
:::
8+
59
## Test Discovery Performance
610

711
### Use AOT Mode
@@ -15,6 +19,10 @@ TUnit's AOT (Ahead-of-Time) compilation mode provides the best performance for t
1519
</PropertyGroup>
1620
```
1721

22+
:::performance Native AOT Performance
23+
TUnit with Native AOT compilation delivers exceptional speed improvements - benchmarks show **11.65x faster** execution compared to regular JIT. See the [AOT benchmarks](/docs/benchmarks) for detailed measurements.
24+
:::
25+
1826
Benefits:
1927
- Faster test discovery
2028
- Lower memory usage

docs/docs/assertions/getting-started.md

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,134 @@ await Assert.That(number).IsEqualTo(42);
250250
// await Assert.That(number).IsEqualTo("42");
251251
```
252252

253+
## Common Mistakes & Best Practices
254+
255+
import Tabs from '@theme/Tabs';
256+
import TabItem from '@theme/TabItem';
257+
258+
### Forgetting to Await
259+
260+
<Tabs>
261+
<TabItem value="bad" label="❌ Bad - Missing Await" default>
262+
263+
```csharp
264+
[Test]
265+
public void TestValue()
266+
{
267+
// Compiler warning: assertion not awaited
268+
Assert.That(result).IsEqualTo(42);
269+
}
270+
```
271+
272+
**Problem:** Assertion never executes, test always passes even if it should fail.
273+
274+
</TabItem>
275+
<TabItem value="good" label="✅ Good - Awaited Properly">
276+
277+
```csharp
278+
[Test]
279+
public async Task TestValue()
280+
{
281+
await Assert.That(result).IsEqualTo(42);
282+
}
283+
```
284+
285+
**Why:** Awaiting ensures the assertion executes and can fail the test.
286+
287+
</TabItem>
288+
</Tabs>
289+
290+
### Comparing Different Types
291+
292+
<Tabs>
293+
<TabItem value="bad" label="❌ Bad - Type Confusion">
294+
295+
```csharp
296+
int number = 42;
297+
// This won't compile - can't compare int to string
298+
// await Assert.That(number).IsEqualTo("42");
299+
300+
// Or this pattern that converts implicitly
301+
string value = GetValue();
302+
await Assert.That(value).IsEqualTo(42); // Won't compile
303+
```
304+
305+
**Problem:** Type mismatches are caught at compile time, preventing runtime surprises.
306+
307+
</TabItem>
308+
<TabItem value="good" label="✅ Good - Explicit Conversion">
309+
310+
```csharp
311+
string value = GetValue();
312+
int parsed = int.Parse(value);
313+
await Assert.That(parsed).IsEqualTo(42);
314+
315+
// Or test the string directly
316+
await Assert.That(value).IsEqualTo("42");
317+
```
318+
319+
**Why:** Be explicit about what you're testing - the string value or its parsed equivalent.
320+
321+
</TabItem>
322+
</Tabs>
323+
324+
### Collection Ordering
325+
326+
<Tabs>
327+
<TabItem value="bad" label="❌ Bad - Assuming Order">
328+
329+
```csharp
330+
var items = GetItemsFromDatabase(); // Order not guaranteed
331+
await Assert.That(items).IsEqualTo(new[] { 1, 2, 3 });
332+
```
333+
334+
**Problem:** Fails unexpectedly if database returns `[3, 1, 2]` even though items are equivalent.
335+
336+
</TabItem>
337+
<TabItem value="good" label="✅ Good - Order-Independent">
338+
339+
```csharp
340+
var items = GetItemsFromDatabase();
341+
await Assert.That(items).IsEquivalentTo(new[] { 1, 2, 3 });
342+
```
343+
344+
**Why:** `IsEquivalentTo` checks for same items regardless of order, making tests more resilient.
345+
346+
</TabItem>
347+
</Tabs>
348+
349+
### Multiple Related Assertions
350+
351+
<Tabs>
352+
<TabItem value="bad" label="❌ Bad - Sequential Assertions">
353+
354+
```csharp
355+
await Assert.That(user.FirstName).IsEqualTo("John");
356+
await Assert.That(user.LastName).IsEqualTo("Doe");
357+
await Assert.That(user.Age).IsGreaterThan(18);
358+
// If first assertion fails, you won't see the other failures
359+
```
360+
361+
**Problem:** Stops at first failure, hiding other issues.
362+
363+
</TabItem>
364+
<TabItem value="good" label="✅ Good - Assert.Multiple">
365+
366+
```csharp
367+
await using (Assert.Multiple())
368+
{
369+
await Assert.That(user.FirstName).IsEqualTo("John");
370+
await Assert.That(user.LastName).IsEqualTo("Doe");
371+
await Assert.That(user.Age).IsGreaterThan(18);
372+
}
373+
// Shows ALL failures at once
374+
```
375+
376+
**Why:** See all failures in one test run, saving debugging time.
377+
378+
</TabItem>
379+
</Tabs>
380+
253381
## Next Steps
254382

255383
Now that you understand the basics, explore specific assertion types:

docs/docs/assertions/library.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
title: Assertions Library
3+
description: Searchable library of all TUnit assertions
4+
sidebar_position: 100
5+
---
6+
7+
import AssertionsLibrary from '@site/src/components/AssertionsLibrary';
8+
9+
<AssertionsLibrary />

docs/docs/intro.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ It provides you a skeleton framework to write, execute and assert tests, with li
88
That means you get more control over your setup, execution, and style of tests.
99

1010
It is also built on top of the newer Microsoft Testing Platform, which was rewritten to make .NET testing simpler and more extensible.
11+
12+
:::performance
13+
TUnit is designed for speed. Through source generation and compile-time optimizations, TUnit significantly outperforms traditional testing frameworks. See the [performance benchmarks](/docs/benchmarks) or try the [benchmark calculator](/docs/benchmarks/calculator) to estimate time savings for your test suite.
14+
:::

docs/docs/migration/mstest.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Migrating from MSTest
22

3+
:::from-mstest Performance Boost
4+
Migrating from MSTest to TUnit can significantly improve test execution speed. Benchmarks show TUnit is **1.3x faster** than MSTest on average. Check the [detailed benchmarks](/docs/benchmarks) to see performance comparisons.
5+
:::
6+
37
## Quick Reference
48

59
| MSTest | TUnit |

docs/docs/migration/nunit.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Migrating from NUnit
22

3+
:::from-nunit Performance Boost
4+
Migrating from NUnit to TUnit can significantly improve test execution speed. Benchmarks show TUnit is **1.2x faster** than NUnit on average. Check the [detailed benchmarks](/docs/benchmarks) to see performance comparisons.
5+
:::
6+
37
## Quick Reference
48

59
| NUnit | TUnit |

docs/docs/migration/xunit.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Migrating from xUnit.net
22

3+
:::from-xunit Performance Boost
4+
Migrating from xUnit to TUnit can significantly improve test execution speed. Benchmarks show TUnit is **1.3x faster** than xUnit v3 on average. Check the [detailed benchmarks](/docs/benchmarks) to see performance comparisons.
5+
:::
6+
37
## Quick Reference
48

59
| xUnit | TUnit |

docs/docs/parallelism/not-in-parallel.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Not in Parallel
22

3-
By default, TUnit tests will run in parallel.
3+
By default, TUnit tests will run in parallel.
4+
5+
:::performance
6+
Parallel execution is a major contributor to TUnit's speed advantage. Running tests in parallel can dramatically reduce total test suite execution time. See the [performance benchmarks](/docs/benchmarks) for real-world performance data.
7+
:::
48

59
To remove this behaviour, we can add a `[NotInParallel]` attribute to our test methods or classes.
610

docs/docs/test-authoring/things-to-know.md

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ Then `MyTest1` and `MyTest2` will have a different instance of `MyTests`.
3131

3232
This isn't that important unless you're storing state.
3333

34-
So you can't do this:
34+
import Tabs from '@theme/Tabs';
35+
import TabItem from '@theme/TabItem';
36+
37+
<Tabs>
38+
<TabItem value="bad" label="❌ Bad - Will Fail" default>
3539

3640
```csharp
3741
public class MyTests
@@ -42,15 +46,19 @@ public class MyTests
4246
public void MyTest1() { _value = 99; }
4347

4448
[Test, NotInParallel]
45-
public async Task MyTest2() { await Assert.That(_value).IsEqualTo(99); }
49+
public async Task MyTest2()
50+
{
51+
// This will FAIL because _value is 0
52+
// Different test instance = different _value
53+
await Assert.That(_value).IsEqualTo(99);
54+
}
4655
}
4756
```
4857

49-
The above will compile fine and run, but it will result in a failing test.
50-
51-
Because `MyTests` in `MyTest2` is different from `MyTests` in `MyTest1`, therefore the `_value` field is a different reference.
58+
**Why this fails:** Each test gets a new instance of `MyTests`, so `_value` in `MyTest2` is a different field than in `MyTest1`.
5259

53-
If you really want to perform a test like the above, you can make your field static, and then that field will persist across any instance. The `static` keyword makes it clear to the user that data persists outside of instances.
60+
</TabItem>
61+
<TabItem value="good" label="✅ Good - Use Static">
5462

5563
```csharp
5664
public class MyTests
@@ -61,6 +69,15 @@ public class MyTests
6169
public void MyTest1() { _value = 99; }
6270

6371
[Test, NotInParallel]
64-
public async Task MyTest2() { await Assert.That(_value).IsEqualTo(99); }
72+
public async Task MyTest2()
73+
{
74+
// This works because _value is static
75+
await Assert.That(_value).IsEqualTo(99);
76+
}
6577
}
6678
```
79+
80+
**Why this works:** The `static` keyword makes the field persist across instances, making it clear that data is shared.
81+
82+
</TabItem>
83+
</Tabs>

0 commit comments

Comments
 (0)