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
102 changes: 102 additions & 0 deletions docs/docs/execution/parameters.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Test Parameters

TUnit allows you to pass custom key-value parameters to your tests at runtime using the `--test-parameter` command-line option. These parameters are accessible via `TestContext.Parameters`.

## Passing Parameters

Pass parameters when running your tests using the `--test-parameter` flag with `KEY=VALUE` syntax:

```bash
dotnet run --test-parameter environment=staging
dotnet run --test-parameter environment=staging --test-parameter api-url=https://api.example.com
```

You can pass multiple values for the same key:

```bash
dotnet run --test-parameter browser=chrome --test-parameter browser=firefox
```

## Accessing Parameters in Tests

Parameters are available as a static dictionary on `TestContext`:

```csharp
public class MyTests
{
[Test]
public async Task ConnectsToCorrectEnvironment()
{
var environments = TestContext.Parameters["environment"];
var environment = environments.First(); // "staging"

// Use the parameter to configure your test
var baseUrl = environment switch
{
"production" => "https://api.example.com",
"staging" => "https://staging.api.example.com",
_ => "http://localhost:5000"
};

// ...
}
}
```

`TestContext.Parameters` is of type `IReadOnlyDictionary<string, List<string>>`. Each key maps to a list of values, since the same key can be specified multiple times on the command line.

## Common Use Cases

### Environment-specific configuration

```csharp
[Before(Test)]
public void SetupEnvironment()
{
if (TestContext.Parameters.TryGetValue("environment", out var values))
{
Environment.SetEnvironmentVariable("TEST_ENV", values.First());
}
}
```

### Conditional test logic

```csharp
[Test]
public async Task IntegrationTest()
{
if (!TestContext.Parameters.ContainsKey("run-integration"))
{
Assert.Skip("Integration tests require --test-parameter run-integration=true");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Skip examples use nonexistent API

When either conditional-skipping example is copied into a current TUnit project, Assert.Skip cannot compile because Assert has no Skip member; TUnit's supported runtime API is Skip.Test. The same invalid call also occurs in the connection-string example on line 86.

Knowledge Base Used: Benchmarks and the Docusaurus Docs Site

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Use the TUnit skip API in the examples

When users copy either conditional-skip example, it will not compile because TUnit has no Assert.Skip method; runtime skipping is exposed as Skip.Test(reason) in src/TUnit.Core/Skip.cs. Update both occurrences so the documented integration and database patterns are usable.

Useful? React with 👍 / 👎.

}

// Run the integration test...
}
```

### Passing secrets or connection strings

```csharp
[Test]
public async Task DatabaseTest()
{
if (!TestContext.Parameters.TryGetValue("connection-string", out var connectionStrings))
{
Assert.Skip("Requires --test-parameter connection-string=...");
}

using var connection = new SqlConnection(connectionStrings.First());
// ...
}
```

```bash
dotnet run --test-parameter "connection-string=Server=localhost;Database=TestDb;..."
```

## Notes

- Parameters are available for the entire test session — they are not scoped to individual tests.
- The parameter format must be `KEY=VALUE`. Values containing `=` characters are supported (only the first `=` is used as the delimiter).

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Equals signs truncate parameter values

When a value contains additional = characters, the current parser stores only the segment immediately after the first delimiter, so the documented connection string becomes Server and the database connection fails. The example and note should reflect the parser's actual limitation or accompany a parser fix that preserves the complete value.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Remove or implement support for additional equals signs

For a value such as token=abc=def, the engine currently calls parameter.Split('=') and stores only split[1] in TUnitProcessInitializer.ParseTestParameters, producing abc rather than abc=def. This note therefore promises behavior the CLI does not provide; either limit the split to two parts in the parser or document the restriction.

Useful? React with 👍 / 👎.

- Parameters are accessible from any test, hook, or data source via `TestContext.Parameters`.
14 changes: 14 additions & 0 deletions docs/docs/writing-tests/test-context.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@ These are useful for any test that needs unique resource names — database tabl
If you're using `TUnit.AspNetCore`, the `WebApplicationTest` base class provides the same helpers as `protected` methods (`GetIsolatedName`, `GetIsolatedPrefix`). Both share the same underlying counter, so IDs are unique across all test types.
:::

## Test Parameters

`TestContext.Parameters` provides access to custom key-value parameters passed at runtime via the `--test-parameter` command-line option:

```csharp
// Run with: dotnet run --test-parameter environment=staging
if (TestContext.Parameters.TryGetValue("environment", out var values))
{
var environment = values.First(); // "staging"
}
```

See the [Test Parameters](../execution/parameters.md) guide for full details.

## Custom Properties

Custom properties can be added to a test using the `[Property]` attribute. Properties are key-value pairs of strings that serve multiple purposes:
Expand Down
1 change: 1 addition & 0 deletions docs/sidebars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ const sidebars: SidebarsConfig = {
collapsed: true,
items: [
'execution/test-filters',
'execution/parameters',
'execution/timeouts',
'execution/retrying',
'execution/repeating',
Expand Down