VOOZH about

URL: https://tunit.dev/docs/getting-started/writing-your-first-test/

⇱ Writing your first test | TUnit


Skip to main content

Quick Start: Complete Example

Here's a complete TUnit test class with all necessary using statements:

usingTUnit.Assertions;
usingTUnit.Assertions.Extensions;
usingTUnit.Core;

namespaceMyTestProject;

publicclassCalculatorTests
{
[Test]
publicasyncTaskAdd_WithTwoNumbers_ReturnsSum()
{
// Arrange
var calculator =newCalculator();

// Act
var result = calculator.Add(2,3);

// Assert
await Assert.That(result).IsEqualTo(5);
}
}

Important: TUnit does not require a [TestClass] attribute. Unlike MSTest or NUnit, you only need the [Test] attribute on your test methods.

Step-by-Step Guide

Auto-Imported Namespaces

The TUnit package automatically configures global usings for TUnit.Core, TUnit.Assertions, and TUnit.Assertions.Extensions. The explicit using statements in the examples below are shown for clarity — you don't need them in practice.

Start by creating a new class:

namespaceMyTestProject;

publicclassMyTestClass
{

}

Now add a method with a [Test] attribute on it:

usingTUnit.Core;

namespaceMyTestProject;

publicclassMyTestClass
{
[Test]
publicasyncTaskMyTest()
{

}
}

That's it. That is your runnable test.

We haven't actually made it do anything yet, but we should be able to build our project and run that test.

Tests will pass if they execute successfully without any exceptions.

Test Method Signatures

Test methods can be either synchronous or asynchronous:

[Test]
publicvoidSynchronousTest()// ✅ Valid - synchronous test
{
var result =Calculate(2,3);
// Simple synchronous test without assertions
}

[Test]
publicasyncTaskAsyncTestWithAssertions()// ✅ Recommended - asynchronous test
{
var result =Calculate(2,3);
await Assert.That(result).IsEqualTo(5);// Assertions must be awaited
}

Important Notes:

  • If you use Assert.That(...), your test must be async Task — assertions return awaitable objects that won't execute without await
  • Synchronous void tests are allowed but cannot use assertions
  • async void tests are not allowed — the TUnit analyzers report this as a build error (diagnostic TUnit0031)

See Awaiting Assertions for more details.

Let's add some code to show you how a test might look once finished:

usingTUnit.Assertions;
usingTUnit.Assertions.Extensions;
usingTUnit.Core;

namespaceMyTestProject;

publicclassMyTestClass
{
[Test]
publicasyncTaskMyTest()
{
var result =Add(1,2);

await Assert.That(result).IsEqualTo(3);
}

privateintAdd(int x,int y)
{
return x + y;
}
}

Here you can see we've executed some code and added an assertion. We'll go more into that later.

Common Test Patterns

Testing Boolean Returns

When testing methods that return boolean values, use IsTrue() or IsFalse():

usingTUnit.Assertions;
usingTUnit.Assertions.Extensions;
usingTUnit.Core;

namespaceMyTestProject;

publicclassValidatorTests
{
[Test]
publicasyncTaskIsPositive_WithNegativeNumber_ReturnsFalse()
{
// Arrange & Act
var result = Validator.IsPositive(-1);

// Assert
await Assert.That(result).IsFalse();
}

[Test]
publicasyncTaskIsPositive_WithPositiveNumber_ReturnsTrue()
{
// Arrange & Act
var result = Validator.IsPositive(5);

// Assert
await Assert.That(result).IsTrue();
}
}

publicstaticclassValidator
{
publicstaticboolIsPositive(int number)
{
return number >0;
}
}

Testing with Multiple Assertions

usingTUnit.Assertions;
usingTUnit.Assertions.Extensions;
usingTUnit.Core;

namespaceMyTestProject;

publicclassStringTests
{
[Test]
publicasyncTaskProcessString_WithValidInput_ReturnsExpectedResult()
{
// Arrange
var input ="hello";

// Act
var result = input.ToUpper();

// Assert
await Assert.That(result).IsEqualTo("HELLO");
await Assert.That(result).Length().IsEqualTo(5);
await Assert.That(result.StartsWith("HE")).IsTrue();
}
}

Next: Run Your Tests →