How to Prevent Sharing Sqlite In-memory Store Among Multiple Test Methods
Technologies: .Net Core, Sqlite Inmemory, C#, Xunit
I'm trying to use Sqlite Inmemory Provider for mocking the database with Xunit. I want to have a fresh store (Set of text data) with each test execution. However Store seems to be sharing between multiple test methods, Hence I'm getting anomalies with test results.
Here is my code. I'm creating a new Sqlite connection with each test execution and disposing it at the end. There is no deference whether test execution is equential and parallel.
[Theory]
[InlineData(1)]
[InlineData(3)]
public async Task WhenPassingValidId_ReturnRecord(int id)
{
using (var connection = new SqliteConnection("DataSource=:memory:"))
{
connection.Open();
var options = DbHelper.GetSqliteContextOptions(connection);
using (IDataContext context = new DataContext(options))
{
await SetupTestDataAsync(context);
}
using (IDataContext context = new DataContext(options))
{
var service = new LookupService(context);
var lookup = await service.GetAsync(id);
Assert.NotNull(lookup);
}
}
}
private async static Task SetupTestDataAsync(IDataContext context)
{
context.LookupHeaders.AddRange(GetLookupHeaders());
await context.SaveChangesAsync();
context.Lookups.AddRange(GetLookups());
await context.SaveChangesAsync();
}
public static DbContextOptions<DataContext> GetSqliteContextOptions(SqliteConnection connection)
{
var options = new DbContextOptionsBuilder<DataContext>()
.UseSqlite(connection)
.Options;
using (var context = new DataContext(options, CreateRequestContext()))
{
context.Database.EnsureCreated();
}
return options;
}
Is there a way to scope Sqlite In-memory database for a single test method?
Thanks.
c# sqlite unit-testing .net-core xunit
add a comment |
Technologies: .Net Core, Sqlite Inmemory, C#, Xunit
I'm trying to use Sqlite Inmemory Provider for mocking the database with Xunit. I want to have a fresh store (Set of text data) with each test execution. However Store seems to be sharing between multiple test methods, Hence I'm getting anomalies with test results.
Here is my code. I'm creating a new Sqlite connection with each test execution and disposing it at the end. There is no deference whether test execution is equential and parallel.
[Theory]
[InlineData(1)]
[InlineData(3)]
public async Task WhenPassingValidId_ReturnRecord(int id)
{
using (var connection = new SqliteConnection("DataSource=:memory:"))
{
connection.Open();
var options = DbHelper.GetSqliteContextOptions(connection);
using (IDataContext context = new DataContext(options))
{
await SetupTestDataAsync(context);
}
using (IDataContext context = new DataContext(options))
{
var service = new LookupService(context);
var lookup = await service.GetAsync(id);
Assert.NotNull(lookup);
}
}
}
private async static Task SetupTestDataAsync(IDataContext context)
{
context.LookupHeaders.AddRange(GetLookupHeaders());
await context.SaveChangesAsync();
context.Lookups.AddRange(GetLookups());
await context.SaveChangesAsync();
}
public static DbContextOptions<DataContext> GetSqliteContextOptions(SqliteConnection connection)
{
var options = new DbContextOptionsBuilder<DataContext>()
.UseSqlite(connection)
.Options;
using (var context = new DataContext(options, CreateRequestContext()))
{
context.Database.EnsureCreated();
}
return options;
}
Is there a way to scope Sqlite In-memory database for a single test method?
Thanks.
c# sqlite unit-testing .net-core xunit
What is the symptoms of Store seems to be sharing between multiple test methods? And what is theStore
?
– Fabio
Jan 2 at 5:38
The database should only exist while the connection is open try closing the connection
– lloyd
Jan 2 at 5:44
Fabio: I have added a unique constraint (Composite) on Lookup Entity. One of the symptom is, when seeding test data it complains about unique constraint violation. Which means someone else has inserted data while another one trying to insert. But when running the failed tests isolately, they execute successfully. Lloyd: Connection is auto closing when disposing at the end of the using block right?
– Wijitha
Jan 2 at 7:24
That's the normal interpretation of the using statement which is correct in almost all cases. However their own example has a try finally block specifically for the connection with using statements inside that block so I suspect in memory may be another exception to the rule.
– lloyd
Jan 3 at 7:26
add a comment |
Technologies: .Net Core, Sqlite Inmemory, C#, Xunit
I'm trying to use Sqlite Inmemory Provider for mocking the database with Xunit. I want to have a fresh store (Set of text data) with each test execution. However Store seems to be sharing between multiple test methods, Hence I'm getting anomalies with test results.
Here is my code. I'm creating a new Sqlite connection with each test execution and disposing it at the end. There is no deference whether test execution is equential and parallel.
[Theory]
[InlineData(1)]
[InlineData(3)]
public async Task WhenPassingValidId_ReturnRecord(int id)
{
using (var connection = new SqliteConnection("DataSource=:memory:"))
{
connection.Open();
var options = DbHelper.GetSqliteContextOptions(connection);
using (IDataContext context = new DataContext(options))
{
await SetupTestDataAsync(context);
}
using (IDataContext context = new DataContext(options))
{
var service = new LookupService(context);
var lookup = await service.GetAsync(id);
Assert.NotNull(lookup);
}
}
}
private async static Task SetupTestDataAsync(IDataContext context)
{
context.LookupHeaders.AddRange(GetLookupHeaders());
await context.SaveChangesAsync();
context.Lookups.AddRange(GetLookups());
await context.SaveChangesAsync();
}
public static DbContextOptions<DataContext> GetSqliteContextOptions(SqliteConnection connection)
{
var options = new DbContextOptionsBuilder<DataContext>()
.UseSqlite(connection)
.Options;
using (var context = new DataContext(options, CreateRequestContext()))
{
context.Database.EnsureCreated();
}
return options;
}
Is there a way to scope Sqlite In-memory database for a single test method?
Thanks.
c# sqlite unit-testing .net-core xunit
Technologies: .Net Core, Sqlite Inmemory, C#, Xunit
I'm trying to use Sqlite Inmemory Provider for mocking the database with Xunit. I want to have a fresh store (Set of text data) with each test execution. However Store seems to be sharing between multiple test methods, Hence I'm getting anomalies with test results.
Here is my code. I'm creating a new Sqlite connection with each test execution and disposing it at the end. There is no deference whether test execution is equential and parallel.
[Theory]
[InlineData(1)]
[InlineData(3)]
public async Task WhenPassingValidId_ReturnRecord(int id)
{
using (var connection = new SqliteConnection("DataSource=:memory:"))
{
connection.Open();
var options = DbHelper.GetSqliteContextOptions(connection);
using (IDataContext context = new DataContext(options))
{
await SetupTestDataAsync(context);
}
using (IDataContext context = new DataContext(options))
{
var service = new LookupService(context);
var lookup = await service.GetAsync(id);
Assert.NotNull(lookup);
}
}
}
private async static Task SetupTestDataAsync(IDataContext context)
{
context.LookupHeaders.AddRange(GetLookupHeaders());
await context.SaveChangesAsync();
context.Lookups.AddRange(GetLookups());
await context.SaveChangesAsync();
}
public static DbContextOptions<DataContext> GetSqliteContextOptions(SqliteConnection connection)
{
var options = new DbContextOptionsBuilder<DataContext>()
.UseSqlite(connection)
.Options;
using (var context = new DataContext(options, CreateRequestContext()))
{
context.Database.EnsureCreated();
}
return options;
}
Is there a way to scope Sqlite In-memory database for a single test method?
Thanks.
c# sqlite unit-testing .net-core xunit
c# sqlite unit-testing .net-core xunit
asked Jan 1 at 16:58


WijithaWijitha
95111
95111
What is the symptoms of Store seems to be sharing between multiple test methods? And what is theStore
?
– Fabio
Jan 2 at 5:38
The database should only exist while the connection is open try closing the connection
– lloyd
Jan 2 at 5:44
Fabio: I have added a unique constraint (Composite) on Lookup Entity. One of the symptom is, when seeding test data it complains about unique constraint violation. Which means someone else has inserted data while another one trying to insert. But when running the failed tests isolately, they execute successfully. Lloyd: Connection is auto closing when disposing at the end of the using block right?
– Wijitha
Jan 2 at 7:24
That's the normal interpretation of the using statement which is correct in almost all cases. However their own example has a try finally block specifically for the connection with using statements inside that block so I suspect in memory may be another exception to the rule.
– lloyd
Jan 3 at 7:26
add a comment |
What is the symptoms of Store seems to be sharing between multiple test methods? And what is theStore
?
– Fabio
Jan 2 at 5:38
The database should only exist while the connection is open try closing the connection
– lloyd
Jan 2 at 5:44
Fabio: I have added a unique constraint (Composite) on Lookup Entity. One of the symptom is, when seeding test data it complains about unique constraint violation. Which means someone else has inserted data while another one trying to insert. But when running the failed tests isolately, they execute successfully. Lloyd: Connection is auto closing when disposing at the end of the using block right?
– Wijitha
Jan 2 at 7:24
That's the normal interpretation of the using statement which is correct in almost all cases. However their own example has a try finally block specifically for the connection with using statements inside that block so I suspect in memory may be another exception to the rule.
– lloyd
Jan 3 at 7:26
What is the symptoms of Store seems to be sharing between multiple test methods? And what is the
Store
?– Fabio
Jan 2 at 5:38
What is the symptoms of Store seems to be sharing between multiple test methods? And what is the
Store
?– Fabio
Jan 2 at 5:38
The database should only exist while the connection is open try closing the connection
– lloyd
Jan 2 at 5:44
The database should only exist while the connection is open try closing the connection
– lloyd
Jan 2 at 5:44
Fabio: I have added a unique constraint (Composite) on Lookup Entity. One of the symptom is, when seeding test data it complains about unique constraint violation. Which means someone else has inserted data while another one trying to insert. But when running the failed tests isolately, they execute successfully. Lloyd: Connection is auto closing when disposing at the end of the using block right?
– Wijitha
Jan 2 at 7:24
Fabio: I have added a unique constraint (Composite) on Lookup Entity. One of the symptom is, when seeding test data it complains about unique constraint violation. Which means someone else has inserted data while another one trying to insert. But when running the failed tests isolately, they execute successfully. Lloyd: Connection is auto closing when disposing at the end of the using block right?
– Wijitha
Jan 2 at 7:24
That's the normal interpretation of the using statement which is correct in almost all cases. However their own example has a try finally block specifically for the connection with using statements inside that block so I suspect in memory may be another exception to the rule.
– lloyd
Jan 3 at 7:26
That's the normal interpretation of the using statement which is correct in almost all cases. However their own example has a try finally block specifically for the connection with using statements inside that block so I suspect in memory may be another exception to the rule.
– lloyd
Jan 3 at 7:26
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53997293%2fhow-to-prevent-sharing-sqlite-in-memory-store-among-multiple-test-methods%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53997293%2fhow-to-prevent-sharing-sqlite-in-memory-store-among-multiple-test-methods%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What is the symptoms of Store seems to be sharing between multiple test methods? And what is the
Store
?– Fabio
Jan 2 at 5:38
The database should only exist while the connection is open try closing the connection
– lloyd
Jan 2 at 5:44
Fabio: I have added a unique constraint (Composite) on Lookup Entity. One of the symptom is, when seeding test data it complains about unique constraint violation. Which means someone else has inserted data while another one trying to insert. But when running the failed tests isolately, they execute successfully. Lloyd: Connection is auto closing when disposing at the end of the using block right?
– Wijitha
Jan 2 at 7:24
That's the normal interpretation of the using statement which is correct in almost all cases. However their own example has a try finally block specifically for the connection with using statements inside that block so I suspect in memory may be another exception to the rule.
– lloyd
Jan 3 at 7:26