xunit 使用技巧 TDD 测试驱动开发
一、项目布局
1、项目
2、xUnit
3、ServicesContract
DTO(数据传输对象 Request和Response)、Enums
4、Services
5、Entities(实体类)
注意:
1、Controllers层和测试层,不接触实体层(Entities),只有Services接触实体层(Entities)
2、Request => Service(Request -> Entities) => Service(Entities -> Response) => Response
3、Resuest 类 中有 ToEntity的方法;Response类中有 Entity实体类的扩展方法(实体对象变成相应对象) 方便
4、相应对象一般重写equals方法,用来比较
二、xUnit
1、创建类与service对应
xxServiceTest.cs
2、方法特性[fact]
3、引用servicec层 构造函数
4、断言方法
a、 Assert.Contians()
b、Assert.Throws<T>()
c、Assert.Equals()
d、Assert.True()
e、Assert.Null()
5、案例
using Service; using ServiceContract; using ServiceContract.DTO; using ServiceContract.Enums; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CRUDTests { public class PersonServiceTest { private readonly IPersonService _personService; public PersonServiceTest() { _personService = new PersonService(); } #region AddPerson [Fact] public void AddPerson_PersonNull() { PersonAddRequest? personAdd = null; Assert.Throws<ArgumentNullException>(() => { _personService.AddPerson(personAdd); }); } [Fact] public void AddPerson_PersonNameNull() { PersonAddRequest personAdd = new PersonAddRequest { PersonName = null }; Assert.Throws<ArgumentException>(() => { _personService.AddPerson(personAdd); }); } [Fact] public void AddPerson_Valid() { PersonAddRequest personAdd = new PersonAddRequest { PersonName = "张三", Email = "zhangsan@163.com", DateOfBirth = DateTime.Parse("2000-01-01"), Gender = GenderOption.Male, CountryId = Guid.NewGuid(), Address = "朝阳大街", ReceiveNewsLetters = true }; PersonResponse personResponse = _personService.AddPerson(personAdd); Assert.True(personResponse.PersonId != Guid.Empty); } #endregion #region GetAllPerson [Fact] public void GetAllPerson_Empty() { Assert.Empty(_personService.GetAllPerson()); } [Fact] public void GetAllPerson_Vaild() { List<PersonAddRequest> list_request_person = new List<PersonAddRequest> { new PersonAddRequest { PersonName = "张三", Email = "zhangsan@163.com", DateOfBirth = DateTime.Parse("2000-01-01"), Gender = GenderOption.Male, CountryId = Guid.NewGuid(), Address = "朝阳大街", ReceiveNewsLetters = true }, new PersonAddRequest { PersonName = "李四", Email = "lisi@163.com", DateOfBirth = DateTime.Parse("2001-01-01"), Gender = GenderOption.FeMale, CountryId = Guid.NewGuid(), Address = "北京大街", ReceiveNewsLetters = true } }; List<PersonResponse> list_response_person = new List<PersonResponse>(); foreach (PersonAddRequest personAddRequest in list_request_person) { list_response_person.Add(_personService.AddPerson(personAddRequest)); } List<PersonResponse> people = _personService.GetAllPerson(); foreach (PersonResponse personResponse in list_response_person) { Assert.Contains(personResponse, people); } } #endregion #region GetPersonByPersonId [Fact] public void GetPersonByPersonId_PersonNull() { Guid? personId = null; Assert.Null(_personService.GetPersonByPersonId(personId)); } [Fact] public void GetPersonByPersonId_Vaild() { PersonAddRequest personAdd = new PersonAddRequest { PersonName = "张三", Email = "zhangsan@163.com", DateOfBirth = DateTime.Parse("2000-01-01"), Gender = GenderOption.Male, CountryId = Guid.NewGuid(), Address = "朝阳大街", ReceiveNewsLetters = true }; PersonResponse personResponse = _personService.AddPerson(personAdd); PersonResponse? search_person = _personService.GetPersonByPersonId(personResponse.PersonId); Assert.Equal(personResponse, search_person); } #endregion } }
6、测试
视图=>测试资源管理器

浙公网安备 33010602011771号