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
1 change: 1 addition & 0 deletions pages/choosing-a-test-strategy.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ A common split:
* Mocks or fakes for domain and business logic.
* LocalDb for data access, queries, migrations, and end-to-end integration tests.
* `[PooledDb]` or `[SharedDb]` for large sets of read-only tests, with a new database per test for anything that writes.
* `[NoDb]` for tests in a `LocalDbTestBase` class that do not touch the database, such as rendering or validation tests. This keeps them next to related database tests without paying for a database.


## Summary
Expand Down
15 changes: 13 additions & 2 deletions pages/ef-mstest-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ public async Task VerifyEntity_Queryable()

Mark test methods with `[SharedDb]` to share a single database across all query-only tests. Instead of cloning the template for each test, a shared database is created once and reused. This eliminates per-test DB creation overhead for tests that only read data.<!-- include: shared-db. path: /pages/mdsource/shared-db.include.md -->

`[SharedDb]` can also be applied to a test class, or to the assembly with `[assembly: SharedDb]`. The nearest attribute wins: a method attribute overrides a class attribute, which overrides an assembly attribute. Mark a test method or class with `[NewDb]` to opt it out and create a database per test. Applying more than one of `[PooledDb]`, `[SharedDb]` and `[NewDb]` to the same method, class, or assembly throws.
`[SharedDb]` can also be applied to a test class, or to the assembly with `[assembly: SharedDb]`. The nearest attribute wins: a method attribute overrides a class attribute, which overrides an assembly attribute. Mark a test method or class with `[NewDb]` to opt it out and create a database per test. Applying more than one of `[PooledDb]`, `[SharedDb]`, `[NewDb]` and `[NoDb]` to the same method, class, or assembly throws.

The shared database is read-only and any write throws, not only `SaveChanges`: `ExecuteUpdate`, `ExecuteDelete`, `ExecuteSqlRaw` and hand-written commands are blocked too. Tests that need to write should use `[PooledDb]` instead.<!-- endInclude -->

Expand All @@ -675,7 +675,7 @@ public class SharedDbTests : LocalDbTestBase<TheDbContext>

Mark test methods with `[PooledDb]` to lease a database from a fixed pool instead of creating one per test. The pool is built once from the template, and each test leases a database for its duration, writes inside a transaction, and rolls that transaction back on release so the next test sees the template state again.<!-- include: pooled-db. path: /pages/mdsource/pooled-db.include.md -->

`[PooledDb]` can also be applied to a test class, or to the assembly with `[assembly: PooledDb]`. The nearest attribute wins: a method attribute overrides a class attribute, which overrides an assembly attribute. Mark a test method or class with `[NewDb]` to opt it out and create a database per test. Applying more than one of `[PooledDb]`, `[SharedDb]` and `[NewDb]` to the same method, class, or assembly throws.
`[PooledDb]` can also be applied to a test class, or to the assembly with `[assembly: PooledDb]`. The nearest attribute wins: a method attribute overrides a class attribute, which overrides an assembly attribute. Mark a test method or class with `[NewDb]` to opt it out and create a database per test. Applying more than one of `[PooledDb]`, `[SharedDb]`, `[NewDb]` and `[NoDb]` to the same method, class, or assembly throws.

Two costs disappear. The per-test file copy and attach is gone, and — usually the larger one — so is repeated query plan compilation: SQL Server keys the plan cache by database, so a database per test means every query is compiled afresh for every test and no plan is ever reused. A small pool lets those plans be reused for the rest of the run.

Expand Down Expand Up @@ -759,6 +759,17 @@ public class PooledDbTests : LocalDbTestBase<TheDbContext>
<!-- endSnippet -->


## NoDb

Mark a test method with `[NoDb]` when it does not touch the database, for example a test that renders a document, formats output, or runs a validator. The test still inherits from `LocalDbTestBase`, so it can live next to related database tests, but no database is built for it.<!-- include: no-db. path: /pages/mdsource/no-db.include.md -->

A test without any attribute gets a new database per test, which is the most expensive mode. So a database-free test in a `LocalDbTestBase` class should be marked `[NoDb]` rather than left unmarked.

In a `[NoDb]` test `Database` is null, and accessing `ArrangeData`, `ActData`, or `AssertData`, or calling `Reset`, throws.

`[NoDb]` can also be applied to a test class. The nearest attribute wins, so a method marked `[PooledDb]`, `[SharedDb]`, or `[NewDb]` in a `[NoDb]` class gets a database. Applying `[NoDb]` together with `[PooledDb]`, `[SharedDb]` or `[NewDb]` to the same method or class throws.<!-- endInclude -->


## Parallel Execution

To run tests in parallel, configure parallelism at the assembly level:
Expand Down
15 changes: 13 additions & 2 deletions pages/ef-nunit-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ public async Task VerifyEntity_Queryable()

Mark test methods with `[SharedDb]` to share a single database across all query-only tests. Instead of cloning the template for each test, a shared database is created once and reused. This eliminates per-test DB creation overhead for tests that only read data.<!-- include: shared-db. path: /pages/mdsource/shared-db.include.md -->

`[SharedDb]` can also be applied to a test class, or to the assembly with `[assembly: SharedDb]`. The nearest attribute wins: a method attribute overrides a class attribute, which overrides an assembly attribute. Mark a test method or class with `[NewDb]` to opt it out and create a database per test. Applying more than one of `[PooledDb]`, `[SharedDb]` and `[NewDb]` to the same method, class, or assembly throws.
`[SharedDb]` can also be applied to a test class, or to the assembly with `[assembly: SharedDb]`. The nearest attribute wins: a method attribute overrides a class attribute, which overrides an assembly attribute. Mark a test method or class with `[NewDb]` to opt it out and create a database per test. Applying more than one of `[PooledDb]`, `[SharedDb]`, `[NewDb]` and `[NoDb]` to the same method, class, or assembly throws.

The shared database is read-only and any write throws, not only `SaveChanges`: `ExecuteUpdate`, `ExecuteDelete`, `ExecuteSqlRaw` and hand-written commands are blocked too. Tests that need to write should use `[PooledDb]` instead.<!-- endInclude -->

Expand Down Expand Up @@ -685,7 +685,7 @@ public class SharedDbTests :

Mark test methods with `[PooledDb]` to lease a database from a fixed pool instead of creating one per test. The pool is built once from the template, and each test leases a database for its duration, writes inside a transaction, and rolls that transaction back on release so the next test sees the template state again.<!-- include: pooled-db. path: /pages/mdsource/pooled-db.include.md -->

`[PooledDb]` can also be applied to a test class, or to the assembly with `[assembly: PooledDb]`. The nearest attribute wins: a method attribute overrides a class attribute, which overrides an assembly attribute. Mark a test method or class with `[NewDb]` to opt it out and create a database per test. Applying more than one of `[PooledDb]`, `[SharedDb]` and `[NewDb]` to the same method, class, or assembly throws.
`[PooledDb]` can also be applied to a test class, or to the assembly with `[assembly: PooledDb]`. The nearest attribute wins: a method attribute overrides a class attribute, which overrides an assembly attribute. Mark a test method or class with `[NewDb]` to opt it out and create a database per test. Applying more than one of `[PooledDb]`, `[SharedDb]`, `[NewDb]` and `[NoDb]` to the same method, class, or assembly throws.

Two costs disappear. The per-test file copy and attach is gone, and — usually the larger one — so is repeated query plan compilation: SQL Server keys the plan cache by database, so a database per test means every query is compiled afresh for every test and no plan is ever reused. A small pool lets those plans be reused for the rest of the run.

Expand Down Expand Up @@ -770,6 +770,17 @@ public class PooledDbTests :
<!-- endSnippet -->


## NoDb

Mark a test method with `[NoDb]` when it does not touch the database, for example a test that renders a document, formats output, or runs a validator. The test still inherits from `LocalDbTestBase`, so it can live next to related database tests, but no database is built for it.<!-- include: no-db. path: /pages/mdsource/no-db.include.md -->

A test without any attribute gets a new database per test, which is the most expensive mode. So a database-free test in a `LocalDbTestBase` class should be marked `[NoDb]` rather than left unmarked.

In a `[NoDb]` test `Database` is null, and accessing `ArrangeData`, `ActData`, or `AssertData`, or calling `Reset`, throws.

`[NoDb]` can also be applied to a test class. The nearest attribute wins, so a method marked `[PooledDb]`, `[SharedDb]`, or `[NewDb]` in a `[NoDb]` class gets a database. Applying `[NoDb]` together with `[PooledDb]`, `[SharedDb]` or `[NewDb]` to the same method or class throws.<!-- endInclude -->


## Parallel Execution

To run tests in parallel, configure parallelism at the assembly level:
Expand Down
15 changes: 13 additions & 2 deletions pages/ef-tunit-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -656,7 +656,7 @@ public async Task VerifyEntity_Queryable()

Mark test methods with `[SharedDb]` to share a single database across all query-only tests. Instead of cloning the template for each test, a shared database is created once and reused. This eliminates per-test DB creation overhead for tests that only read data.<!-- include: shared-db. path: /pages/mdsource/shared-db.include.md -->

`[SharedDb]` can also be applied to a test class, or to the assembly with `[assembly: SharedDb]`. The nearest attribute wins: a method attribute overrides a class attribute, which overrides an assembly attribute. Mark a test method or class with `[NewDb]` to opt it out and create a database per test. Applying more than one of `[PooledDb]`, `[SharedDb]` and `[NewDb]` to the same method, class, or assembly throws.
`[SharedDb]` can also be applied to a test class, or to the assembly with `[assembly: SharedDb]`. The nearest attribute wins: a method attribute overrides a class attribute, which overrides an assembly attribute. Mark a test method or class with `[NewDb]` to opt it out and create a database per test. Applying more than one of `[PooledDb]`, `[SharedDb]`, `[NewDb]` and `[NoDb]` to the same method, class, or assembly throws.

The shared database is read-only and any write throws, not only `SaveChanges`: `ExecuteUpdate`, `ExecuteDelete`, `ExecuteSqlRaw` and hand-written commands are blocked too. Tests that need to write should use `[PooledDb]` instead.<!-- endInclude -->

Expand All @@ -682,7 +682,7 @@ public class SharedDbTests : LocalDbTestBase<TheDbContext>

Mark test methods with `[PooledDb]` to lease a database from a fixed pool instead of creating one per test. The pool is built once from the template, and each test leases a database for its duration, writes inside a transaction, and rolls that transaction back on release so the next test sees the template state again.<!-- include: pooled-db. path: /pages/mdsource/pooled-db.include.md -->

`[PooledDb]` can also be applied to a test class, or to the assembly with `[assembly: PooledDb]`. The nearest attribute wins: a method attribute overrides a class attribute, which overrides an assembly attribute. Mark a test method or class with `[NewDb]` to opt it out and create a database per test. Applying more than one of `[PooledDb]`, `[SharedDb]` and `[NewDb]` to the same method, class, or assembly throws.
`[PooledDb]` can also be applied to a test class, or to the assembly with `[assembly: PooledDb]`. The nearest attribute wins: a method attribute overrides a class attribute, which overrides an assembly attribute. Mark a test method or class with `[NewDb]` to opt it out and create a database per test. Applying more than one of `[PooledDb]`, `[SharedDb]`, `[NewDb]` and `[NoDb]` to the same method, class, or assembly throws.

Two costs disappear. The per-test file copy and attach is gone, and — usually the larger one — so is repeated query plan compilation: SQL Server keys the plan cache by database, so a database per test means every query is compiled afresh for every test and no plan is ever reused. A small pool lets those plans be reused for the rest of the run.

Expand Down Expand Up @@ -766,6 +766,17 @@ public class PooledDbTests : LocalDbTestBase<TheDbContext>
<!-- endSnippet -->


## NoDb

Mark a test method with `[NoDb]` when it does not touch the database, for example a test that renders a document, formats output, or runs a validator. The test still inherits from `LocalDbTestBase`, so it can live next to related database tests, but no database is built for it.<!-- include: no-db. path: /pages/mdsource/no-db.include.md -->

A test without any attribute gets a new database per test, which is the most expensive mode. So a database-free test in a `LocalDbTestBase` class should be marked `[NoDb]` rather than left unmarked.

In a `[NoDb]` test `Database` is null, and accessing `ArrangeData`, `ActData`, or `AssertData`, or calling `Reset`, throws.

`[NoDb]` can also be applied to a test class. The nearest attribute wins, so a method marked `[PooledDb]`, `[SharedDb]`, or `[NewDb]` in a `[NoDb]` class gets a database. Applying `[NoDb]` together with `[PooledDb]`, `[SharedDb]` or `[NewDb]` to the same method or class throws.<!-- endInclude -->


## Parallel Execution

To run tests in parallel, configure parallelism at the assembly level:
Expand Down
15 changes: 13 additions & 2 deletions pages/ef-xunitv3-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ public async Task VerifyEntity_Queryable()

Mark test methods with `[SharedDb]` to share a single database across all query-only tests. Instead of cloning the template for each test, a shared database is created once and reused. This eliminates per-test DB creation overhead for tests that only read data.<!-- include: shared-db. path: /pages/mdsource/shared-db.include.md -->

`[SharedDb]` can also be applied to a test class, or to the assembly with `[assembly: SharedDb]`. The nearest attribute wins: a method attribute overrides a class attribute, which overrides an assembly attribute. Mark a test method or class with `[NewDb]` to opt it out and create a database per test. Applying more than one of `[PooledDb]`, `[SharedDb]` and `[NewDb]` to the same method, class, or assembly throws.
`[SharedDb]` can also be applied to a test class, or to the assembly with `[assembly: SharedDb]`. The nearest attribute wins: a method attribute overrides a class attribute, which overrides an assembly attribute. Mark a test method or class with `[NewDb]` to opt it out and create a database per test. Applying more than one of `[PooledDb]`, `[SharedDb]`, `[NewDb]` and `[NoDb]` to the same method, class, or assembly throws.

The shared database is read-only and any write throws, not only `SaveChanges`: `ExecuteUpdate`, `ExecuteDelete`, `ExecuteSqlRaw` and hand-written commands are blocked too. Tests that need to write should use `[PooledDb]` instead.<!-- endInclude -->

Expand All @@ -681,7 +681,7 @@ public class SharedDbTests : LocalDbTestBase<TheDbContext>

Mark test methods with `[PooledDb]` to lease a database from a fixed pool instead of creating one per test. The pool is built once from the template, and each test leases a database for its duration, writes inside a transaction, and rolls that transaction back on release so the next test sees the template state again.<!-- include: pooled-db. path: /pages/mdsource/pooled-db.include.md -->

`[PooledDb]` can also be applied to a test class, or to the assembly with `[assembly: PooledDb]`. The nearest attribute wins: a method attribute overrides a class attribute, which overrides an assembly attribute. Mark a test method or class with `[NewDb]` to opt it out and create a database per test. Applying more than one of `[PooledDb]`, `[SharedDb]` and `[NewDb]` to the same method, class, or assembly throws.
`[PooledDb]` can also be applied to a test class, or to the assembly with `[assembly: PooledDb]`. The nearest attribute wins: a method attribute overrides a class attribute, which overrides an assembly attribute. Mark a test method or class with `[NewDb]` to opt it out and create a database per test. Applying more than one of `[PooledDb]`, `[SharedDb]`, `[NewDb]` and `[NoDb]` to the same method, class, or assembly throws.

Two costs disappear. The per-test file copy and attach is gone, and — usually the larger one — so is repeated query plan compilation: SQL Server keys the plan cache by database, so a database per test means every query is compiled afresh for every test and no plan is ever reused. A small pool lets those plans be reused for the rest of the run.

Expand Down Expand Up @@ -764,6 +764,17 @@ public class PooledDbTests : LocalDbTestBase<TheDbContext>
<!-- endSnippet -->


## NoDb

Mark a test method with `[NoDb]` when it does not touch the database, for example a test that renders a document, formats output, or runs a validator. The test still inherits from `LocalDbTestBase`, so it can live next to related database tests, but no database is built for it.<!-- include: no-db. path: /pages/mdsource/no-db.include.md -->

A test without any attribute gets a new database per test, which is the most expensive mode. So a database-free test in a `LocalDbTestBase` class should be marked `[NoDb]` rather than left unmarked.

In a `[NoDb]` test `Database` is null, and accessing `ArrangeData`, `ActData`, or `AssertData`, or calling `Reset`, throws.

`[NoDb]` can also be applied to a test class. The nearest attribute wins, so a method marked `[PooledDb]`, `[SharedDb]`, or `[NewDb]` in a `[NoDb]` class gets a database. Applying `[NoDb]` together with `[PooledDb]`, `[SharedDb]` or `[NewDb]` to the same method or class throws.<!-- endInclude -->


## Parallel Execution

To run tests in parallel, configure parallelism at the assembly level:
Expand Down
1 change: 1 addition & 0 deletions pages/mdsource/choosing-a-test-strategy.source.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ A common split:
* Mocks or fakes for domain and business logic.
* LocalDb for data access, queries, migrations, and end-to-end integration tests.
* `[PooledDb]` or `[SharedDb]` for large sets of read-only tests, with a new database per test for anything that writes.
* `[NoDb]` for tests in a `LocalDbTestBase` class that do not touch the database, such as rendering or validation tests. This keeps them next to related database tests without paying for a database.


## Summary
Expand Down
5 changes: 5 additions & 0 deletions pages/mdsource/ef-mstest-usage.source.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ include: pooled-db
snippet: PooledDbTestsMSTest


## NoDb

include: no-db


## Parallel Execution

To run tests in parallel, configure parallelism at the assembly level:
Expand Down
5 changes: 5 additions & 0 deletions pages/mdsource/ef-nunit-usage.source.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ include: pooled-db
snippet: PooledDbTestsNUnit


## NoDb

include: no-db


## Parallel Execution

To run tests in parallel, configure parallelism at the assembly level:
Expand Down
5 changes: 5 additions & 0 deletions pages/mdsource/ef-tunit-usage.source.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ include: pooled-db
snippet: PooledDbTestsTUnit


## NoDb

include: no-db


## Parallel Execution

To run tests in parallel, configure parallelism at the assembly level:
Expand Down
5 changes: 5 additions & 0 deletions pages/mdsource/ef-xunitv3-usage.source.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ include: pooled-db
snippet: PooledDbTestsXunitV3


## NoDb

include: no-db


## Parallel Execution

To run tests in parallel, configure parallelism at the assembly level:
Expand Down
7 changes: 7 additions & 0 deletions pages/mdsource/no-db.include.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Mark a test method with `[NoDb]` when it does not touch the database, for example a test that renders a document, formats output, or runs a validator. The test still inherits from `LocalDbTestBase`, so it can live next to related database tests, but no database is built for it.

A test without any attribute gets a new database per test, which is the most expensive mode. So a database-free test in a `LocalDbTestBase` class should be marked `[NoDb]` rather than left unmarked.

In a `[NoDb]` test `Database` is null, and accessing `ArrangeData`, `ActData`, or `AssertData`, or calling `Reset`, throws.

`[NoDb]` can also be applied to a test class. The nearest attribute wins, so a method marked `[PooledDb]`, `[SharedDb]`, or `[NewDb]` in a `[NoDb]` class gets a database. Applying `[NoDb]` together with `[PooledDb]`, `[SharedDb]` or `[NewDb]` to the same method or class throws.
Loading
Loading