我试图使用EF7 InMemory提供程序进行单元测试,但测试之间的InMemory数据库的持久性使我遇到问题.
以下代码演示了我的问题.一个测试将起作用,另一个测试将始终失败.即使我在测试之间将_context设置为null,第二次测试运行总会有4条记录.
[TestClass]
public class UnitTest1
{
private SchoolContext _context;
[TestInitialize]
public void Setup()
{
Random rng = new Random();
var optionsBuilder = new DbContextOptionsBuilder<SchoolContext>();
optionsBuilder.UseInMemoryDatabase();
_context = new SchoolContext(optionsBuilder.Options);
_context.Students.AddRange(
new Student { Id = rng.Next(1,10000), Name = "Able" },
new Student { Id = rng.Next(1,10000), Name = "Bob" }
);
_context.SaveChanges();
}
[TestCleanup]
public void Cleanup()
{
_context = null;
}
[TestMethod]
public void TestMethod1()
{
Assert.AreEqual(2, _context.Students.ToList().Count());
}
[TestMethod]
public void TestMethod2()
{
Assert.AreEqual(2, _context.Students.ToList().Count());
} …Run Code Online (Sandbox Code Playgroud) MSTest是否具有类似于nUnit的独立GUI,让我可以使用它并在没有visual studio的情况下运行测试?什么是MSTest的官方网站,我可以在那里了解更多如何使用它?
我们必须决定使用哪种技术进行单元测试.目前我们使用的是Visual Studio 2010,并不满意MSTest.它是错误的,部署较差(例如,无法正确识别测试设置输出目录),并且在尝试测试32位和64位版本的程序集时有几个问题.最糟糕的是,MSTest与我们的Jenkins构建系统没有很好的阻抗匹配.因此我们考虑进入NUnit.但是,我们团队中没有人对NUnit有很好的了解.此外,我们将很快进入Visual Studio 2012.
我需要了解Visual Studio 2012 MSTest与Nunit最新版本的优缺点.由于堆栈溢出的大多数文章都与旧版本的VS相关,因此它们与我们无关.我想微软自2010年以来已经对MSTest做了很多改进.请提供一个与两种技术所面临的细节技术问题的公正比较(仅限新版本)
我无法调试或单步测试.
这是我的示例测试代码......
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DomainModel.Entities;
using DomainModel.Abstract;
using WebUI.Controllers;
namespace Tests
{
[TestClass]
public class PeopleControllerTests
{
static IPeopleRepository MockPeopleRepository(params Person[] people)
{
var mockPeopleRepos = new Moq.Mock<IPeopleRepository>();
mockPeopleRepos.Setup(x => x.People).Returns(people.AsQueryable());
return mockPeopleRepos.Object;
}
[TestMethod]
public void Count_Of_People()
{
IPeopleRepository repository = MockPeopleRepository(
new Person { Age = 31, Gender = "Male", Name = "Tom" },
new Person { Age = 25, Gender = "Female", Name = "Sally" },
new Person …Run Code Online (Sandbox Code Playgroud) NUnit支持一项功能,您可以在其中指定一组数据输入,以便多次运行单元测试.
[RowTest]
[Row(1001,1,2,3)]
[Row(1,1001,2,3)]
[Row(1,2,1001,3)]
public void SumTests(int x, int y, int z, int expected)
{
...
}
Run Code Online (Sandbox Code Playgroud)
使用MSTest完成同类事物的最佳方法是什么?我找不到类似的属性集.
我是一个新的单元测试,使用Microsoft.VisualStudio.TestTools.UnitTesting;
该0.GetType()实际上是System.RuntimeType,那么什么样的测试,我需要写通Assert.IsInstanceOfType(0.GetType(), typeof(int))?
---跟进,这是我自己的用户错误... Assert.IsInstanceOfType(0, typeof(int))
我目前正将我的MsTest单元测试转换为xUnit.使用xUnit,有没有办法测试异常消息?测试异常消息是否正确,而不仅仅是异常类型?
使用resharper从visual studio运行单元测试时出现以下错误:
Test-case objects missing for the following elements: ... Rebuild the project and try again
在单元测试方法旁边的工具提示中,它说:Inconclusive: Test not run我认为它曾经说过Unit test skipped.Resharper显示灰色眼睛图标.
Visual Studio的Test Explorer也存在一个奇怪的问题.它不会显示我的所有单元测试.事实上,它缺少200多个单元测试.它们只是没有出现在Test Explorer窗口中.我确实有一些测试出现并且运行得很好.
我已经尝试过的事情:重新启动我的机器,清理,重建,更改所有依赖项目以使用相同的.net框架4.7.
我正在使用VS 2017 .net Framework 4.7,Resharper和MSTest.全部包含最新的更新和版本.
我在Visual Studio 2010中编写了一些单元测试.我可以使用"在当前上下文中运行所有测试"来运行所有测试.
但是,如果我编写一个新的单元测试,它不会被环境拾取 - 换句话说,我无法在测试列表编辑器中找到它,通过运行所有测试或其他任何地方.如果我卸载项目然后重新加载它; 新测试可以运行.
当我添加单元测试时,我只是向已经存在的TestClass添加一个新方法并使用[TestMethod]属性进行装饰 - 没什么特别的.
可能导致此行为的原因,以及如何使其工作?