C# Nunit 和 运行 每次测试前都会填充相同的数据库
C# Nunit and Running same database populate everytime before test
我有一个 Entity Framework 数据库上下文文件。
在 运行 进行测试之前,我将如何设置 DBContext 和 运行 SetUp 每次(以清理填充的数据库)?它在 NUnit 测试中看不到 dbcontext。
电子数据库上下文文件
public partial class ElectronicsContext : DbContext
{
public ElectronicsContext()
{
}
public ElectronicsContext(DbContextOptions<ElectronicsContext> options)
: base(options)
{
}
public virtual DbSet<Product> Product { get; set; }
public virtual DbSet<ProductCategory> ProductCategory { get; set; }
Nunit 设置
public class TestClass
{
[SetUp]
public void TestProducts()
{
var options = new DbContextOptionsBuilder<ElectronicsContext>()
.UseInMemoryDatabase(databaseName: "Products Test")
.Options;
using (var context = new ElectronicsContext(options))
{
context.Product.Add(new Product { ProductId = 1, ProductName = "TV", ProductDescription = "TV testing", ImageLocation = "test" , ProductCategoryId = 2});
context.SaveChanges();
}
}
这里有错误信息
The name 'context' does not exist in the current context
[Test]
public void TestProductRepository()
{
ProductRepository productRepository = new ProductRepository(context);
Assert.AreEqual("TV", productRepository.GetById(1).ProductName);
}
}
设置的替代方案也不起作用:
[SetUp]
public void TestProducts()
{
var options = new DbContextOptionsBuilder<ElectronicsContext>()
.UseInMemoryDatabase(databaseName: "Products Test")
.Options;
ElectronicsContext context = new ElectronicsContext(options);
创建测试 class 的私有成员,可以在 SetUp
方法中实例化并在测试方法中使用。
因为 NUnit 将为 class 中的所有测试创建一次 TestClass 实例,您不能使用 readonly
成员。
应为每个测试重新分配(重置)上下文,以使它们彼此隔离。
public class TestClass
{
private DbContext _context;
[SetUp]
public void SetUp()
{
var options =
new DbContextOptionsBuilder<ElectronicsContext>()
.UseInMemoryDatabase(databaseName: "Products Test")
.Options;
// new instance of ElectronicsContext will be created for every test.
_context = new ElectronicsContext(options);
// Use _context to insert initial data required for the test
}
[TearDown]
public void TearDown()
{
_context.Dispose();
}
[Test]
public void RunTest()
{
// run test
// assert
_context.Products.Count().Should().Be(10)
}
}
假设测试夹具只是一个 c# class,其中标有 Test
属性的方法按顺序执行。
var test = new TestClass();
// first test
test.SetUp(); // create db context
test.RunTest(); // use db context
test.TearDown(); // dispose db context
// second test
test.SetUp(); // create db context
test.RunTest_Two(); // use db context
test.TearDown(); // dispose db context
我有一个 Entity Framework 数据库上下文文件。 在 运行 进行测试之前,我将如何设置 DBContext 和 运行 SetUp 每次(以清理填充的数据库)?它在 NUnit 测试中看不到 dbcontext。
电子数据库上下文文件
public partial class ElectronicsContext : DbContext
{
public ElectronicsContext()
{
}
public ElectronicsContext(DbContextOptions<ElectronicsContext> options)
: base(options)
{
}
public virtual DbSet<Product> Product { get; set; }
public virtual DbSet<ProductCategory> ProductCategory { get; set; }
Nunit 设置
public class TestClass
{
[SetUp]
public void TestProducts()
{
var options = new DbContextOptionsBuilder<ElectronicsContext>()
.UseInMemoryDatabase(databaseName: "Products Test")
.Options;
using (var context = new ElectronicsContext(options))
{
context.Product.Add(new Product { ProductId = 1, ProductName = "TV", ProductDescription = "TV testing", ImageLocation = "test" , ProductCategoryId = 2});
context.SaveChanges();
}
}
这里有错误信息
The name 'context' does not exist in the current context
[Test]
public void TestProductRepository()
{
ProductRepository productRepository = new ProductRepository(context);
Assert.AreEqual("TV", productRepository.GetById(1).ProductName);
}
}
设置的替代方案也不起作用:
[SetUp]
public void TestProducts()
{
var options = new DbContextOptionsBuilder<ElectronicsContext>()
.UseInMemoryDatabase(databaseName: "Products Test")
.Options;
ElectronicsContext context = new ElectronicsContext(options);
创建测试 class 的私有成员,可以在 SetUp
方法中实例化并在测试方法中使用。
因为 NUnit 将为 class 中的所有测试创建一次 TestClass 实例,您不能使用 readonly
成员。
应为每个测试重新分配(重置)上下文,以使它们彼此隔离。
public class TestClass
{
private DbContext _context;
[SetUp]
public void SetUp()
{
var options =
new DbContextOptionsBuilder<ElectronicsContext>()
.UseInMemoryDatabase(databaseName: "Products Test")
.Options;
// new instance of ElectronicsContext will be created for every test.
_context = new ElectronicsContext(options);
// Use _context to insert initial data required for the test
}
[TearDown]
public void TearDown()
{
_context.Dispose();
}
[Test]
public void RunTest()
{
// run test
// assert
_context.Products.Count().Should().Be(10)
}
}
假设测试夹具只是一个 c# class,其中标有 Test
属性的方法按顺序执行。
var test = new TestClass();
// first test
test.SetUp(); // create db context
test.RunTest(); // use db context
test.TearDown(); // dispose db context
// second test
test.SetUp(); // create db context
test.RunTest_Two(); // use db context
test.TearDown(); // dispose db context