Skip to content

Commit

Permalink
Update Hooks Docs (#1259)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomhurst authored Nov 20, 2024
1 parent 0f9026b commit ea7a475
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 10 deletions.
38 changes: 33 additions & 5 deletions docs/docs/tutorial-extras/cleanup.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,42 @@ TUnit supports having your test class implement `IDisposable` or `IAsyncDisposab

You can also declare a method with an `[After(...)]` or an `[AfterEvery(...)]` attribute.

- `[After(Test)]` methods should NOT be static, and they will be executed repeatedly after each test in their class ends.
- `[After(Class)]` methods SHOULD be static, and they will be executed only once, after all tests in their class end.
- `[After(Assembly)]` methods SHOULD be static, and they will be executed only once, after all tests in their assembly end.
## [After(HookType)]

### [After(Test)]
Must be an instance method. Will be executed after each test in the class it's defined in.
Methods will be executed top-down, so the current class clean ups will execute first, then the base classes' last.

- All `[AfterEvery(...)]` methods SHOULD be static, and they will follow the same behaviour as above, but fire for every test/class/assembly that is being run in the test session.
### [After(Class)]
Must be a static method. Will run once after the last test in the class it's defined it finishes.

Methods will be executed top-down, so the current class clean ups will execute first, then the base classes' last.
### [After(Assembly)]
Must be a static method. Will run once after the last test in the assembly it's defined it finishes.

### [After(TestSession)]
Must be a static method. Will run once after the last test in the test session finishes.

### [After(TestDiscovery)]
Must be a static method. Will run once after tests are discovered.

## [AfterEvery(HookType)]
All [AfterEvery(...)] methods must be static - And should ideally be placed in their own file that's easy to find, as they can globally affect the test suite, so it should be easy for developers to locate this behaviour.
e.g. `GlobalHooks.cs` at the root of the test project.

### [AfterEvery(Test)]
Will be executed after every test that will run in the test session.

### [AfterEvery(Class)]
Will be executed after the last test of every class that will run in the test session.

### [AfterEvery(Assembly)]
Will be executed after the last test of every assembly that will run in the test session.

### [AfterEvery(TestSession)]
The same as [After(TestSession)]

### [AfterEvery(TestDiscovery)]
The same as [After(TestDiscovery)]

```csharp
using TUnit.Core;
Expand Down
38 changes: 33 additions & 5 deletions docs/docs/tutorial-extras/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,42 @@ E.g. pinging a service to wake it up in preparation for the tests.

For this, we can declare a method with a `[Before(...)]` or a `[BeforeEvery(...)]` attribute.

- `[Before(Test)]` methods should NOT be static, and they will be executed repeatedly before each test in their class starts.
- `[Before(Class)]` methods SHOULD be static, and they will be executed only once, before any test in their class starts.
- `[Before(Assembly)]` methods SHOULD be static, and they will be executed only once, before any test in their assembly starts.
## [Before(HookType)]

### [Before(Test)]
Must be an instance method. Will be executed before each test in the class it's defined in.
Methods will be executed bottom-up, so the base class set ups will execute first and then the inheriting classes.

- All `[BeforeEvery(...)]` methods SHOULD be static, and they will follow the same behaviour as above, but fire for every test/class/assembly that is being run in the test session.
### [Before(Class)]
Must be a static method. Will run once before the first test in the class it's defined it starts.

Methods will be executed bottom-up, so the base class set ups will execute first and then the inheriting classes.
### [Before(Assembly)]
Must be a static method. Will run once before the first test in the assembly it's defined it starts.

### [Before(TestSession)]
Must be a static method. Will run once before the first test in the test session starts.

### [Before(TestDiscovery)]
Must be a static method. Will run once before any tests are discovered.

## [BeforeEvery(HookType)]
All [BeforeEvery(...)] methods must be static - And should ideally be placed in their own file that's easy to find, as they can globally affect the test suite, so it should be easy for developers to locate this behaviour.
e.g. `GlobalHooks.cs` at the root of the test project.

### [BeforeEvery(Test)]
Will be executed before every test that will run in the test session.

### [BeforeEvery(Class)]
Will be executed before the first test of every class that will run in the test session.

### [BeforeEvery(Assembly)]
Will be executed before the first test of every assembly that will run in the test session.

### [BeforeEvery(TestSession)]
The same as [Before(TestSession)]

### [BeforeEvery(TestDiscovery)]
The same as [Before(TestDiscovery)]

```csharp
using TUnit.Core;
Expand Down

0 comments on commit ea7a475

Please sign in to comment.