运行 单元独立测试 (.net core)
Running unit tests independently (.net core )
有人知道我们可以使用什么类型的属性来 运行 独立地进行所有单元测试吗?例如,在下面我们使用了 [Fact] 属性,但它不会独立地进行测试 运行。是否有任何其他类型属性可用于在每次测试开始时初始化数据并使测试 运行 彼此独立?我如何 运行 在 visual studio 代码中独立进行单元测试?
namespace Tests
{
public class TestCategory
{
//Test Get() Method
[Fact]
public void Get_WhenCalled_ReturnsAllCategories()
{
//Arrange
//create _options
var _Options = new DbContextOptionsBuilder<MyContext>()
.UseInMemoryDatabase("Data Source=MyCart.db").Options;
var Context = new MyContext(_Options);
Context.CategoryTestData();//We make sure that dummy data has been added
var Controller = new CategoryController(Context);//pass context inside controller
//Act
var Results = Controller.Get();//call Get() function inside Category controller
//Assert
Assert.NotNull(Results);
}
//Test GetById() Method
//When valid Id is passed
[Fact]
public void GetById_ExistingIntIdPassed_ReturnsOkResult()
{
//Arrange
var _Options = new DbContextOptionsBuilder<MyContext>()
.UseInMemoryDatabase("Data Source=MyCart.db").Options;
var Context = new MyContext(_Options);//pass _Options into context
var Controller = new CategoryController(Context);//pass Context inside controller
//Act
var OkResult = Controller.GetById(1);//1 is valid Id
//Assert
Assert.IsType<OkObjectResult>(OkResult.Result);
}
}
}
如果您正在使用 Visual Studio IDE,您应该尝试检查
运行 在测试资源管理器 window 上并行测试。
然后你的测试应该 运行 并行。
如果您向 class 添加构造函数,您可以设置“初始化”数据等。
没有[属性],如果你知道其他测试框架,可以看看a comparison list,看看[属性] 与 xUnit 相比。
当您查看该页面上的 *注 3 时,您会看到它描述了可用于共享 context between tests
对于parallel testing you can configure it in the configuration of xUnit。
我终于在这里找到了答案:
实际上我们希望我们的上下文不在测试之间共享,因此我们必须在每个测试开始时创建一个新的上下文,如下所示:
using (var Context = new myContext(CreateNewContextOptions()))
{
//here we implement Arrange, Act, and Assert parts
}
CreateNewContextOptions()其实是一个帮助我们创建新上下文的函数
有人知道我们可以使用什么类型的属性来 运行 独立地进行所有单元测试吗?例如,在下面我们使用了 [Fact] 属性,但它不会独立地进行测试 运行。是否有任何其他类型属性可用于在每次测试开始时初始化数据并使测试 运行 彼此独立?我如何 运行 在 visual studio 代码中独立进行单元测试?
namespace Tests
{
public class TestCategory
{
//Test Get() Method
[Fact]
public void Get_WhenCalled_ReturnsAllCategories()
{
//Arrange
//create _options
var _Options = new DbContextOptionsBuilder<MyContext>()
.UseInMemoryDatabase("Data Source=MyCart.db").Options;
var Context = new MyContext(_Options);
Context.CategoryTestData();//We make sure that dummy data has been added
var Controller = new CategoryController(Context);//pass context inside controller
//Act
var Results = Controller.Get();//call Get() function inside Category controller
//Assert
Assert.NotNull(Results);
}
//Test GetById() Method
//When valid Id is passed
[Fact]
public void GetById_ExistingIntIdPassed_ReturnsOkResult()
{
//Arrange
var _Options = new DbContextOptionsBuilder<MyContext>()
.UseInMemoryDatabase("Data Source=MyCart.db").Options;
var Context = new MyContext(_Options);//pass _Options into context
var Controller = new CategoryController(Context);//pass Context inside controller
//Act
var OkResult = Controller.GetById(1);//1 is valid Id
//Assert
Assert.IsType<OkObjectResult>(OkResult.Result);
}
}
}
如果您正在使用 Visual Studio IDE,您应该尝试检查
运行 在测试资源管理器 window 上并行测试。
然后你的测试应该 运行 并行。
如果您向 class 添加构造函数,您可以设置“初始化”数据等。
没有[属性],如果你知道其他测试框架,可以看看a comparison list,看看[属性] 与 xUnit 相比。
当您查看该页面上的 *注 3 时,您会看到它描述了可用于共享 context between tests
对于parallel testing you can configure it in the configuration of xUnit。
我终于在这里找到了答案:
using (var Context = new myContext(CreateNewContextOptions()))
{
//here we implement Arrange, Act, and Assert parts
}
CreateNewContextOptions()其实是一个帮助我们创建新上下文的函数