xunit地址:https://github.com/xunit/xunit
一、利用请求来测试接口,主要是测试webapi控制器方法
①添加xunit项目 ,然后引用我们的主项目,nuget:
Microsoft.AspNetCore.TestHost,
Microsoft.AspNetCore.Mvc.Testing(不然找不到接口地址),
Microsoft.NETCore.App,
Microsoft.AspNetCore.App ,
Microsoft.EntityFrameworkCore.InMemory 使用内存数据库进行测试
②在主项目中写几个测试接口
③测试项目中创建 TestServerFixture
public class TestServerFixture : IDisposable { private readonly TestServer _testServer; public HttpClient Client { get; } public TestServerFixture() { var bulild = new WebHostBuilder().UseStartup<Startup1>(); _testServer = new TestServer(bulild); Client = _testServer.CreateClient(); } public void Dispose() { Client.Dispose(); _testServer.Dispose(); } }
然后创建 Startup1类跟主项目中保持一致。连接数据库的方式换成内存数据库,注:如果 有使用到IConfiguration,把路径写死成主项目的路径,然后得到IConfiguration
根据路径获取IConfiguration的方法:
//path 设置到住项目的路径即可
IConfigurationBuilder builder = new ConfigurationBuilder() .SetBasePath(path) ////Microsoft.Extensions.Configuration.FileExtensions .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);//Microsoft.Extensions.Configuration.Json AddJsonFile if (!string.IsNullOrWhiteSpace(environmentName)) { builder = builder.AddJsonFile($"appsettings.{environmentName}.json", optional: true); } builder = builder.AddEnvironmentVariables(); //Microsoft.Extensions.Configuration.EnvironmentVariables return builder.Build();
创建测试,运行就可以了:
授权的怎么调用:列如:接口使用的JWT授权
在两个启动文件中加入,两个都需要加
然后在测试的TestServerFixture 中添加头部信息
如果通过了授权:
二、测试仓储层代码,不走webapi,主要使用注入和解耦来实现依赖注入 ,需要引用:Autofac
创建DbFixture.cs来实现注入和数据库使用
public class DbFixture { public IContainer Container { get; private set; } public DbFixture() { var builder = new ContainerBuilder(); //内存数据库 var option = new DbContextOptionsBuilder<MyDbContext>().UseInMemoryDatabase("My.D3").Options; MyDbContext context = new MyDbContext(option); //InitializeDbForTests 初始化测试数据 new TestDataBuilder(context).Build(); builder.RegisterInstance(context).As<MyDbContext>(); //注入 Server.ContentRootPath = Path.GetFullPath(@"..\..\..\..\..\") + @"src\My.D3"; IConfigurationRoot configuration = AppConfigurationHelper.Get(Server.ContentRootPath); builder.RegisterType<SimpleDbContextProvider<MyDbContext>>().As<IDbContextProvider<MyDbContext>>().InstancePerLifetimeScope(); var assemblysServices = Assembly.Load("My.D3.Application"); builder.RegisterAssemblyTypes(assemblysServices).AsImplementedInterfaces(); builder.RegisterAssemblyTypes(typeof(DbFixture).GetTypeInfo().Assembly); Container = builder.Build(); } }
测试仓储层案列:
/// <summary> /// 集成测试 测试appservice层接口 /// </summary> public class Test2_Test : IClassFixture<DbFixture> { private readonly IEnumAppService _enumAppService; public Test2_Test(DbFixture fixture) { this._enumAppService = fixture.Container.Resolve<IEnumAppService>(); } /// <summary> /// 测试 /// </summary> /// <param name="inDto"></param> /// <returns></returns> [Fact] public async Task Test() { EnumInDto inDto = new EnumInDto() { EnumTypeName = "SexTypeEnum", Module = "Demo" }; var list = await _enumAppService.GetEnumArray(inDto); string aa = "222"; } }
这样就可以了,测试webapi和仓储层是不一样的,一般我们使用第一种集成测试就好了。