EfCore.TestSupport
1.0.0
See the version list below for details.
dotnet add package EfCore.TestSupport --version 1.0.0
NuGet\Install-Package EfCore.TestSupport -Version 1.0.0
<PackageReference Include="EfCore.TestSupport" Version="1.0.0" />
paket add EfCore.TestSupport --version 1.0.0
#r "nuget: EfCore.TestSupport, 1.0.0"
// Install EfCore.TestSupport as a Cake Addin #addin nuget:?package=EfCore.TestSupport&version=1.0.0 // Install EfCore.TestSupport as a Cake Tool #tool nuget:?package=EfCore.TestSupport&version=1.0.0
EfCore.TestSupport
This git repo contains the source of the NuGet package EfCore.TestSupport, and various tests to check that NuGet package.
Documentation
This netstandard2.0 library contains tools to help anyone that is unit testing applications that use Entity Framework Core for database access. The techniques are explained in chapter 15 of the book Entity Framework in Action. This readme defines the various groups and the signatures of the methods.
The various groups of tools are
- Helpers to create an in-memory Sqlite database for unit testing
- Helpers to create an InMemory database for unit testing
- Helpers to create connection strings with a unique database name
- Helpers for creating unique SQL Server databases for unit testing
- Helpers for creating an empty database, and deleting SQL unit test databases
- Various tools for getting test data, or file paths to test data
- A tool for applying a SQL script file to a EF Core database
- Tools for capturing EF Core logging
1. Helpers to create an in-memory Sqlite database
The sqliteInMemory.CreateOptions<T>
method will create options that will
provide a sqlite, in-memory database for unit testing. The code below shows
how is can be used.
[Fact]
public void TestSqliteOk()
{
//SETUP
var options = SqliteInMemory.CreateOptions<EfCoreContext>();
using (var context = new EfCoreContext(options))
{
//... rest of unit test goes here
It has one, optional bool parameter of throwOnClientServerWarning
which defaults to false.
If set to true it will configure EF Core to throw an expection if a QueryClientEvaluationWarning
is logged.
2. Helpers to create an InMemory database
The EfInMemory.CreateOptions<T>
method will create options that will
provide an EF Core InMemory database for unit testing. The code below shows
how is can be used.
[Fact]
public void TestSqliteOk()
{
//SETUP
var options = EfInMemory.CreateOptions<EfCoreContext>();
using (var context = new EfCoreContext(options))
{
//... rest of unit test goes here
It has one, optional bool parameter of throwOnClientServerWarning
which defaults to false.
If set to true it will configure EF Core to throw an expection if a QueryClientEvaluationWarning
is logged.
3. Helpers to create connection strings with a unique database name
The xUnit unit test library will run unit test classes in parallel.
This means you need class-unique databases to allow the unit tests not to clash.
I have a number of methods to help with this, but first you must add a appsettings.json
Specifying the base database connection string
If you are going to use this library to help create SQL Server databases,
then you need to place an appsettings.json
file in the top-level directory
of you test project. The file should contain:
- A connection string with the name
UnitTestConnection
- The name of the database in that connection string must end with
-Test
. That is a safety feature (see later)
Click here
for an example of the appsettings.json
file.
The AppSettings.GetConfiguration()
method
The method AppSettings.GetConfiguration()
will get the configuration file using the ASP.NET Core code.
You can place any setting for your unit tests
The GetUniqueDatabaseConnectionString()
extention method
The method GetUniqueDatabaseConnectionString()
is an extention method on an object.
It uses that object's name to form a connection string based on the UnitTestConnection
in
you appsettings.json
file, but where the database name from the UnitTestConnection
connection string has the name of the object added as a suffix. See the test code below
(Note: the class the unit test is in is called TestAppSettings
)
[Fact]
public void GetTestConnectionStringOk()
{
//SETUP
var config = AppSettings.GetConfiguration();
var orgDbName = new SqlConnectionStringBuilder(config.GetConnectionString(AppSettings.UnitTestConnectionStringName)).InitialCatalog;
//ATTEMPT
var con = this.GetUniqueDatabaseConnectionString();
//VERIFY
var newDatabaseName = new SqlConnectionStringBuilder(con).InitialCatalog;
newDatabaseName.ShouldEqual ($"{orgDbName}.{typeof(TestAppSettings).Name}");
}
The GetUniqueDatabaseConnectionString()
extention method takes one, optional
parameter, which it will add onto the database name. This allows you to make
method-level unique database names
4. Helpers for creating unique SQL Server databases
The library has two methods that will create options to provide an SQL Server database for unit testing. One provides a class-level unique database name, and one provides a method-unique database name.
Both take the same optional parameter as the Sqlite.CreateOptions<T> which defaults to false.
If set to true it will configure EF Core to throw an expection if a QueryClientEvaluationWarning
is logged.
The CreateUniqueClassOptions()
extension method
This returns a SQL Server options with the connection string from the appsettings.json
file
but the name of the database now has the type name of the object (which should be this
)
as a suffix. See test code below
[Fact]
public void TestSqlServerUniqueClassOk()
{
//SETUP
//ATTEMPT
var options = this.CreateUniqueClassOptions<EfCoreContext>();
using (var context = new EfCoreContext(options))
{
//VERIFY
var builder = new SqlConnectionStringBuilder(context.Database.GetDbConnection().ConnectionString);
builder.InitialCatalog.ShouldEndWith(this.GetType().Name);
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.EntityFrameworkCore.InMemory (>= 2.0.0)
- Microsoft.EntityFrameworkCore.Sqlite (>= 2.0.0)
- Microsoft.EntityFrameworkCore.SqlServer (>= 2.0.0)
- Microsoft.Extensions.Configuration (>= 2.0.0)
- Microsoft.Extensions.Configuration.Json (>= 2.0.0)
- System.Data.SqlClient (>= 4.4.0)
- xunit (>= 2.2.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories (8)
Showing the top 5 popular GitHub repositories that depend on EfCore.TestSupport:
Repository | Stars |
---|---|
JonPSmith/AuthPermissions.AspNetCore
This library provides extra authorization and multi-tenant features to an ASP.NET Core application.
|
|
JonPSmith/EfCore.GenericServices
A library to help you quickly code CRUD accesses for a web/mobile/desktop application using EF Core.
|
|
JonPSmith/EfCoreinAction-SecondEdition
Supporting repo to go with book "Entity Framework Core in Action", second edition
|
|
JonPSmith/PermissionAccessControl2
Version 2 of example application to go with articles on feature and data authorization
|
|
JonPSmith/PermissionAccessControl
Example code for Authorization articles
|
Version | Downloads | Last updated |
---|---|---|
9.0.0 | 1,753 | 11/20/2024 |
8.0.1 | 133,234 | 4/10/2024 |
8.0.0 | 1,500 | 4/4/2024 |
6.0.2 | 41,289 | 2/7/2024 |
6.0.1 | 14,903 | 1/23/2024 |
6.0.0 | 55,707 | 11/21/2023 |
5.3.0 | 224,855 | 11/14/2022 |
5.2.2 | 360,664 | 1/4/2022 |
5.2.1 | 23,089 | 11/19/2021 |
5.2.0 | 1,235 | 11/16/2021 |
5.1.0 | 12,279 | 11/10/2021 |
5.1.0-preview001 | 215 | 11/4/2021 |
5.0.0 | 214,095 | 1/2/2021 |
5.0.0-preview006 | 1,003 | 12/11/2020 |
5.0.0-preview005 | 773 | 12/10/2020 |
5.0.0-preview004 | 875 | 12/10/2020 |
5.0.0-preview003 | 839 | 12/5/2020 |
5.0.0-preview002 | 880 | 12/5/2020 |
5.0.0-preview001 | 852 | 12/5/2020 |
3.2.0 | 179,139 | 5/23/2020 |
3.1.1 | 31,881 | 3/26/2020 |
3.1.0 | 60,628 | 11/1/2019 |
3.0.0 | 8,803 | 10/12/2019 |
2.0.1 | 50,177 | 7/22/2019 |
2.0.0 | 31,480 | 6/4/2019 |
1.9.0 | 155,399 | 4/22/2019 |
1.8.0 | 5,202 | 3/25/2019 |
1.7.0 | 2,717 | 3/19/2019 |
1.6.1 | 11,806 | 1/28/2019 |
1.6.0 | 1,524 | 1/28/2019 |
1.5.2 | 17,540 | 1/4/2019 |
1.5.1 | 46,529 | 10/24/2018 |
1.5.0 | 4,220 | 10/2/2018 |
1.4.0 | 14,531 | 6/7/2018 |
1.3.1 | 8,593 | 4/1/2018 |
1.3.0 | 2,705 | 3/1/2018 |
1.2.0 | 2,124 | 1/22/2018 |
1.1.5 | 1,966 | 12/27/2017 |
1.1.4 | 1,795 | 12/27/2017 |
1.1.3 | 2,009 | 12/1/2017 |
1.1.2 | 1,679 | 11/21/2017 |
1.1.1 | 1,697 | 11/17/2017 |
1.1.0 | 1,684 | 11/16/2017 |
1.0.2 | 1,653 | 10/13/2017 |
1.0.1 | 1,703 | 9/30/2017 |
1.0.0 | 2,074 | 9/29/2017 |
First release