-
-
Notifications
You must be signed in to change notification settings - Fork 129
docs: Add documentation for TestContext.Parameters #6558
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When users copy either conditional-skip example, it will not compile because TUnit has no 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). | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a value contains additional 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! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For a value such as Useful? React with 👍 / 👎. |
||
| - Parameters are accessible from any test, hook, or data source via `TestContext.Parameters`. | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When either conditional-skipping example is copied into a current TUnit project,
Assert.Skipcannot compile becauseAsserthas noSkipmember; TUnit's supported runtime API isSkip.Test. The same invalid call also occurs in the connection-string example on line 86.Knowledge Base Used: Benchmarks and the Docusaurus Docs Site