-
-
Notifications
You must be signed in to change notification settings - Fork 108
Add Skip.Test() documentation for dynamic test skipping #4018
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -64,3 +64,158 @@ public class MyTestClass | |||||
| { | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| ## Dynamic Skipping at Runtime | ||||||
|
|
||||||
| Sometimes you need to determine whether to skip a test at runtime based on conditions that aren't known until the test executes. For this, you can use the static `Skip.Test(reason)` method. | ||||||
|
|
||||||
| ### Skip.Test() | ||||||
|
|
||||||
| The `Skip.Test(reason)` method allows you to dynamically skip a test from within the test method or hooks. When called, it throws a `SkipTestException` that the test framework catches and marks the test as skipped. | ||||||
|
|
||||||
| ```csharp | ||||||
| using TUnit.Core; | ||||||
|
|
||||||
| namespace MyTestProject; | ||||||
|
|
||||||
| public class MyTestClass | ||||||
| { | ||||||
| [Test] | ||||||
| public async Task MyTest() | ||||||
| { | ||||||
| var apiAvailable = await CheckApiAvailability(); | ||||||
|
|
||||||
| if (!apiAvailable) | ||||||
| { | ||||||
| Skip.Test("API is not available"); | ||||||
| } | ||||||
|
|
||||||
| // Test continues only if API is available | ||||||
| await CallApi(); | ||||||
| } | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| ### Skip.When() | ||||||
|
|
||||||
| For cleaner conditional skipping, you can use `Skip.When(condition, reason)` which skips the test when the condition is `true`: | ||||||
|
|
||||||
| ```csharp | ||||||
| using TUnit.Core; | ||||||
|
|
||||||
| namespace MyTestProject; | ||||||
|
|
||||||
| public class MyTestClass | ||||||
| { | ||||||
| [Test] | ||||||
| public void MyTest() | ||||||
| { | ||||||
| var isCI = Environment.GetEnvironmentVariable("CI") != null; | ||||||
|
|
||||||
| Skip.When(isCI, "This test doesn't run in CI environments"); | ||||||
|
|
||||||
| // Test continues only when not in CI | ||||||
| RunLocalOnlyTest(); | ||||||
| } | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| ### Skip.Unless() | ||||||
|
|
||||||
| Similarly, `Skip.Unless(condition, reason)` skips the test unless the condition is `true`: | ||||||
|
|
||||||
| ```csharp | ||||||
| using TUnit.Core; | ||||||
|
|
||||||
| namespace MyTestProject; | ||||||
|
|
||||||
| public class MyTestClass | ||||||
| { | ||||||
| [Test] | ||||||
| public void MyTest() | ||||||
| { | ||||||
| var hasRequiredPermissions = CheckPermissions(); | ||||||
|
|
||||||
| Skip.Unless(hasRequiredPermissions, "User doesn't have required permissions"); | ||||||
|
|
||||||
| // Test continues only if user has permissions | ||||||
|
||||||
| // Test continues only if user has permissions | |
| // Test continues only if user has permissions. |
Copilot
AI
Dec 9, 2025
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.
Inconsistent punctuation in comment. The comment should end with a period for consistency with other comments in the documentation (e.g., line 93, 172, 206).
Suggested change:
// This test will be skipped if database is unavailable.| // This test will be skipped if database is unavailable | |
| // This test will be skipped if database is unavailable. |
Copilot
AI
Dec 9, 2025
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.
Inconsistent punctuation in comment. The comment should end with a period for consistency with other comments in the documentation (e.g., line 93, 172, 206).
Suggested change:
// This test will also be skipped if database is unavailable.| // This test will also be skipped if database is unavailable | |
| // This test will also be skipped if database is unavailable. |
Copilot
AI
Dec 9, 2025
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.
Inconsistent punctuation in comment. The comment should end with a period for consistency with other comments in the documentation (e.g., line 93, 172, 206).
Suggested change:
// All tests in this class will be skipped if service is unavailable.| // All tests in this class will be skipped if service is unavailable | |
| // All tests in this class will be skipped if service is unavailable. |
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.
Inconsistent punctuation in comment. The comment should end with a period for consistency with other comments in the documentation (e.g., line 93, 172, 206).
Suggested change:
// Test continues only when not in CI.