更新:添加2017年; VS2017使用与2015年相同的测试发现/运行器集成机制,因此可能出错的关键事项是相同的.
我已经阅读了为什么xUnit运行器没有找到我的测试,这涵盖了xUnit 永远无法找到你的测试但我的问题不同的原因 - 我相信我的测试没有任何微妙的变化; (他们在其他环境中工作过,这似乎只是我的机器) - Visual Studio 2015中的Visual Studio Test Runner [社区版]根本没有显示我的任何测试.我没有做任何令人兴奋的事情; 测试针对桌面上的xUnit.net v2.
我查看了"输出"窗口,在" 显示"选项卡的"输出"中的" 测试"下看不到任何内容.
我在.NET Core项目中有一个大型测试套件.我可以使用Test Explorer窗口选择要运行的几个测试.
我也可以在命令行上运行整个测试套件dotnet test.有没有办法在命令行上只运行一个(或几个)测试?
当您使用[Theory]连同[InlineData]它会创建提供了在线数据的每个项目的测试.但是,如果您使用[MemberData]它,它将只显示为一个测试.
有没有办法让[MemberData]测试显示为多个测试?
上下文
在XUnit github中我发现了这个:添加Assert.Equal(预期,实际,消息)重载#350 (所以开发人员要求不存在的重载请参见下文)
引用答案:
我们相信自我记录的代码; 包括你的断言.
(所以XUnit团队拒绝它)
好,我知道了.我也相信自我记录代码.我还是找不到这个用例:
样品
// Arrange
// Create some external soap service client and its wrapper classes
// Act
// client.SomeMethod();
// Assert
// Sorry, soap service's interface, behaviour and design is *given*
// So I have to check if there is no Error, and
// conveniently if there is, then I would like to see it in the assertion message
Assert.Equal(0, client.ErrorMessage.Length); // Means no error
// I would like to have …Run Code Online (Sandbox Code Playgroud) 我正在使用xUnit 2.0 集合夹具来共享许多不同测试类之间的公共数据库设置/拆卸.fixture还提供了一些辅助属性,因此我将它注入每个测试类.
我在文档中重新创建了示例,但是当我运行测试时,它立即失败:
以下构造函数参数没有匹配的fixture数据:IntegrationTestFixture fixture
无论我是使用xUnit Facts还是使用Theories,或者我正在使用哪个测试运行器,这似乎都会发生.
夹具:
public class IntegrationTestFixture : IDisposable
{
public IntegrationTestFixture()
{
// (setup code)
this.GeneratedTestName = [randomly generated];
}
public void Dispose()
{
// (teardown code)
}
public string GeneratedTestName { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)
集合定义:
[CollectionDefinition("Live tests")]
public class IntegrationTestCollection : ICollectionFixture<IntegrationTestFixture>
{
// Intentionally left blank.
// This class only serves as an anchor for CollectionDefinition.
}
Run Code Online (Sandbox Code Playgroud)
测试:
[CollectionDefinition("Live tests")]
public class SomeTests
{
private readonly IntegrationTestFixture fixture;
public …Run Code Online (Sandbox Code Playgroud) 我正在按照本教程
使用Entity Framework Core和SQL Server进行集成测试
我的代码看起来像这样
集成测试类
public class ControllerRequestsShould : IDisposable
{
private readonly TestServer _server;
private readonly HttpClient _client;
private readonly YourContext _context;
public ControllerRequestsShould()
{
// Arrange
var serviceProvider = new ServiceCollection()
.AddEntityFrameworkSqlServer()
.BuildServiceProvider();
var builder = new DbContextOptionsBuilder<YourContext>();
builder.UseSqlServer($"Server=(localdb)\\mssqllocaldb;Database=your_db_{Guid.NewGuid()};Trusted_Connection=True;MultipleActiveResultSets=true")
.UseInternalServiceProvider(serviceProvider);
_context = new YourContext(builder.Options);
_context.Database.Migrate();
_server = new TestServer(new WebHostBuilder()
.UseStartup<Startup>()
.UseEnvironment(Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")));
_client = _server.CreateClient();
}
[Fact]
public async Task ReturnListOfObjectDtos()
{
// Arrange database data
_context.ObjectDbSet.Add(new ObjectEntity{ Id = 1, Code = "PTF0001", Name = …Run Code Online (Sandbox Code Playgroud) c# integration-testing entity-framework-core xunit2 asp.net-core
我试图在XUnit项目中执行以下操作,以获取我的测试应该使用的数据库的连接字符串:
public class TestFixture : IDisposable
{
public IConfigurationRoot Configuration { get; set; }
public MyFixture()
{
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
Configuration = builder.Build();
}
public void Dispose()
{
}
}
Run Code Online (Sandbox Code Playgroud)
这很奇怪,因为它在Startup.cs中使用时在WebAPI和MVC模板中完美运行.此外,此代码以前在RC1中使用dnx工作,但现在我将所有内容更新到RC2和Core CLI,它不再能够找到appsettings.jsonxunit类库的根目录中的文件.
这是我的测试类的样子,所以你可以看到我如何调用配置:
public class MyTests : IClassFixture<MyFixture>
{
private readonly MyFixture _fixture;
public MyTests(MyFixture fixture)
{
this._fixture = fixture;
}
[Fact]
public void TestCase1()
{
ICarRepository carRepository = new CarRepository(_fixture.Configuration);
}
}
Run Code Online (Sandbox Code Playgroud) 我真的很沮丧这个问题.我已经尝试更改版本号,但测试资源管理器中没有显示任何测试.
在测试输出窗口中,我可以看到此输出
Starting Microsoft.Framework.TestHost [C:\Users\sul\.dnx\runtimes\dnx-clr-win-x86.1.0.0-beta4\bin\dnx.exe --appbase "path to test project" Microsoft.Framework.ApplicationHost --port 63938 Microsoft.Framework.TestHost --port 63954 list ]
Unable to start Microsoft.Framework.TestHost
========== Discover test finished: 0 found (0:00:36.5471185) ==========
Run Code Online (Sandbox Code Playgroud)
这是如此脆弱,有时测试显示然后他们消失了.重启VS没有帮助,重新安装xunit/xunit运行器也没有帮助.
在另一个测试项目中,我得到了不同的输出,但仍未显示测试
Starting Microsoft.Framework.TestHost [C:\Users\sul\.dnx\runtimes\dnx-clr-win-x86.1.0.0-beta4\bin\dnx.exe --appbase "path to project" Microsoft.Framework.ApplicationHost --port 63938 Microsoft.Framework.TestHost --port 64421 list ]
Connected to Microsoft.Framework.TestHost
Discovering tests in 'path to project\project.json'
========== Discover test finished: 0 found (0:00:35.9341416) ==========
Run Code Online (Sandbox Code Playgroud)
这是我的project.json的一部分
"commands": {
"test": "xunit.runner.dnx"
},
"dependencies": {
"Microsoft.AspNet.Http": "1.0.0-*",
"Microsoft.AspNet.Http.Core": "1.0.0-*",
"Microsoft.AspNet.TestHost": "1.0.0-*",
"Moq": "4.2.1502.911", …Run Code Online (Sandbox Code Playgroud) 我有使用xUnit的这个示例测试:
[Fact]
public void SomeTest()
{
Assert.All(itemList, async item=>
{
var i = await Something(item);
Assert.Equal(item,i);
});
}
Run Code Online (Sandbox Code Playgroud)
是否有一个很好的解决方案可以使整个测试异步/等待?
当我今天使用xUnit v2运行我的测试时,我通常使用如下命名约定:
[Fact(DisplayName= "Method Will Do Something")]
public void Method_Will_Do_Something() { }
我可以插入什么扩展点,这将允许我根据我的测试方法的命名约定设置我的测试显示名称?
xunit2 ×10
c# ×5
xunit.net ×5
xunit ×4
.net ×2
.net-core ×2
asp.net-core ×2
unit-testing ×2
asp.net ×1
assertions ×1
dnx ×1