相信你們在看到單元測試與集成測試這個標題時,會有不少感慨,咱們無數次的在實踐中提到要作單元測試、集成測試,可是大多數項目都沒有作或者僅建了項目文件。這裏有客觀緣由,已經接近交付日期了,咱們沒時間作白盒測試了。也有主觀緣由,面對業務複雜的代碼咱們不知道如何入手作單元測試,不如就留給黑盒測試吧。可是,當咱們的代碼沒法進行單元測試的時候,每每就是代碼開始散發出壞味道的時候。久而久之,將欠下技術債務。在實踐過程當中,技術債務經常會存在,關鍵在於什麼時候償還,如何償還。html
上圖說明了隨着時間的推移開發/維護難度的變化。git
在 .NET Core 中,提供了 xUnit 、NUnit 、 MSTest 三種單元測試框架。github
MSTest | UNnit | xUnit | 說明 | 提示 |
---|---|---|---|---|
[TestMethod] | [Test] | [Fact] | 標記一個測試方法 | |
[TestClass] | [TestFixture] | n/a | 標記一個 Class 爲測試類,xUnit 不須要標記特性,它將查找程序集下全部 Public 的類 | |
[ExpectedException] | [ExpectedException] | Assert.Throws 或者 Record.Exception | xUnit 去掉了 ExpectedException 特性,支持 Assert.Throws | |
[TestInitialize] | [SetUp] | Constructor | 咱們認爲使用 [SetUp] 一般來講很差。可是,你能夠實現一個無參構造器直接替換 [SetUp]。 | 有時咱們會在多個測試方法中用到相同的變量,熟悉重構的咱們會提取公共變量,並在構造器中初始化。可是,這裏我要強調的是:在測試中,不要提取公共變量,這會破壞每一個測試用例的隔離性以及單一職責原則。 |
[TestCleanup] | [TearDown] | IDisposable.Dispose | 咱們認爲使用 [TearDown] 一般來講很差。可是你能夠實現 IDisposable.Dispose 以替換。 | [TearDown] 和 [SetUp] 一般成對出現,在 [SetUp] 中初始化一些變量,則在 [TearDown] 中銷燬這些變量。 |
[ClassInitialize] | [TestFixtureSetUp] | IClassFixture< T > | 共用前置類 | 這裏 IClassFixture< T > 替換了 IUseFixture< T > ,參考 |
[ClassCleanup] | [TestFixtureTearDown] | IClassFixture< T > | 共用後置類 | 同上 |
[Ignore] | [Ignore] | [Fact(Skip="reason")] | 在 [Fact] 特性中設置 Skip 參數以臨時跳過測試 | |
[Timeout] | [Timeout] | [Fact(Timeout=n)] | 在 [Fact] 特性中設置一個 Timeout 參數,當容許時間太長時引發測試失敗。注意,xUnit 的單位時毫秒。 | |
[DataSource] | n/a | [Theory], [XxxData] | Theory(數據驅動測試),表示執行相同代碼,但具備不一樣輸入參數的測試套件 | 這個特性能夠幫助咱們少寫不少代碼。 |
以上寫了 MSTest 、UNnit 、 xUnit 的特性以及比較,能夠看出 xUnit 在使用上相對其它兩個框架來講提供更多的便利性。可是這裏最終實現仍是看我的習慣以選擇。數據庫
新建單元測試項目
json
新建 Class
api
添加測試方法網絡
/// <summary> /// 添加地址 /// </summary> /// <returns></returns> [Fact] public async Task Add_Address_ReturnZero() { DbContextOptions<AddressContext> options = new DbContextOptionsBuilder<AddressContext>().UseInMemoryDatabase("Add_Address_Database").Options; var addressContext = new AddressContext(options); var createAddress = new AddressCreateDto { City = "昆明", County = "五華區", Province = "雲南省" }; var stubAddressRepository = new Mock<IRepository<Domain.Address>>(); var stubProvinceRepository = new Mock<IRepository<Province>>(); var addressUnitOfWork = new AddressUnitOfWork<AddressContext>(addressContext); var stubAddressService = new AddressServiceImpl.AddressServiceImpl(stubAddressRepository.Object, stubProvinceRepository.Object, addressUnitOfWork); await stubAddressService.CreateAddressAsync(createAddress); int addressAmountActual = await addressContext.Addresses.CountAsync(); Assert.Equal(1, addressAmountActual); }
打開視圖 -> 測試資源管理器。
app
點擊運行,獲得測試結果。
框架
至此,一個單元測試結束。async
集成測試確保應用的組件功能在包含應用的基礎支持下是正確的,例如:數據庫、文件系統、網絡等。
新建集成測試項目。
添加工具類 Utilities 。
using System.Collections.Generic; using AddressEFRepository; namespace Address.IntegrationTest { public static class Utilities { public static void InitializeDbForTests(AddressContext db) { List<Domain.Address> addresses = GetSeedingAddresses(); db.Addresses.AddRange(addresses); db.SaveChanges(); } public static void ReinitializeDbForTests(AddressContext db) { db.Addresses.RemoveRange(db.Addresses); InitializeDbForTests(db); } public static List<Domain.Address> GetSeedingAddresses() { return new List<Domain.Address> { new Domain.Address { City = "貴陽", County = "測試縣", Province = "貴州省" }, new Domain.Address { City = "昆明市", County = "武定縣", Province = "雲南省" }, new Domain.Address { City = "昆明市", County = "五華區", Province = "雲南省" } }; } } }
添加 CustomWebApplicationFactory 類,
using System; using System.IO; using System.Linq; using AddressEFRepository; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; namespace Address.IntegrationTest { public class CustomWebApplicationFactory<TStartup> : WebApplicationFactory<TStartup> where TStartup : class { protected override void ConfigureWebHost(IWebHostBuilder builder) { string projectDir = Directory.GetCurrentDirectory(); string configPath = Path.Combine(projectDir, "appsettings.json"); builder.ConfigureAppConfiguration((context, conf) => { conf.AddJsonFile(configPath); }); builder.ConfigureServices(services => { ServiceDescriptor descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<AddressContext>)); if (descriptor != null) { services.Remove(descriptor); } services.AddDbContextPool<AddressContext>((options, context) => { //var configuration = options.GetRequiredService<IConfiguration>(); //string connectionString = configuration.GetConnectionString("TestAddressDb"); //context.UseMySql(connectionString); context.UseInMemoryDatabase("InMemoryDbForTesting"); }); // Build the service provider. ServiceProvider sp = services.BuildServiceProvider(); // Create a scope to obtain a reference to the database // context (ApplicationDbContext). using IServiceScope scope = sp.CreateScope(); IServiceProvider scopedServices = scope.ServiceProvider; var db = scopedServices.GetRequiredService<AddressContext>(); var logger = scopedServices.GetRequiredService<ILogger<CustomWebApplicationFactory<TStartup>>>(); // Ensure the database is created. db.Database.EnsureCreated(); try { // Seed the database with test data. Utilities.ReinitializeDbForTests(db); } catch (Exception ex) { logger.LogError(ex, "An error occurred seeding the " + "database with test messages. Error: {Message}", ex.Message); } }); } } }
添加集成測試 AddressControllerIntegrationTest 類。
using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Threading.Tasks; using Address.Api; using Microsoft.AspNetCore.Mvc.Testing; using Newtonsoft.Json; using Xunit; namespace Address.IntegrationTest { public class AddressControllerIntegrationTest : IClassFixture<CustomWebApplicationFactory<Startup>> { public AddressControllerIntegrationTest(CustomWebApplicationFactory<Startup> factory) { _client = factory.CreateClient(new WebApplicationFactoryClientOptions { AllowAutoRedirect = false }); } private readonly HttpClient _client; [Fact] public async Task Get_AllAddressAndRetrieveAddress() { const string allAddressUri = "/api/Address/GetAll"; HttpResponseMessage allAddressesHttpResponse = await _client.GetAsync(allAddressUri); allAddressesHttpResponse.EnsureSuccessStatusCode(); string allAddressStringResponse = await allAddressesHttpResponse.Content.ReadAsStringAsync(); var addresses = JsonConvert.DeserializeObject<IList<AddressDto.AddressDto>>(allAddressStringResponse); Assert.Equal(3, addresses.Count); AddressDto.AddressDto address = addresses.First(); string retrieveUri = $"/api/Address/Retrieve?id={address.ID}"; HttpResponseMessage addressHttpResponse = await _client.GetAsync(retrieveUri); // Must be successful. addressHttpResponse.EnsureSuccessStatusCode(); // Deserialize and examine results. string addressStringResponse = await addressHttpResponse.Content.ReadAsStringAsync(); var addressResult = JsonConvert.DeserializeObject<AddressDto.AddressDto>(addressStringResponse); Assert.Equal(address.ID, addressResult.ID); Assert.Equal(address.Province, addressResult.Province); Assert.Equal(address.City, addressResult.City); Assert.Equal(address.County, addressResult.County); } } }
在測試資源管理器中運行集成測試方法。
結果。
至此,集成測試完成。須要注意的是,集成測試每每耗時比較多,因此建議能使用單元測試時就不要使用集成測試。
總結:當咱們寫單元測試時,通常不會同時存在 Stub 和 Mock 兩種模擬對象,當同時出現這兩種對象時,代表單元測試寫的不合理,或者業務寫的太過龐大,同時,咱們能夠經過單元測試驅動業務代碼重構。當須要重構時,咱們應儘可能完成重構,不要留下欠下過多技術債務。集成測試有自身的複雜度存在,咱們不要節約時間而打破單一職責原則,不然會引起不可預期後果。爲了應對業務修改,咱們應該在業務修改之後,進行迴歸測試,迴歸測試主要關注被修改的業務部分,同時測試用例若是有沒要能夠重寫,運行整個和修改業務有關的測試用例集。