webAPI xUnit单元测试
通常我们在编码完成功能后,都需要进行单元测试,编写单元测试的好处在于有助于代码维护。一、测试框架MSTest与xUnit比较特性比较MSTest xUnit 备注
无 标记为测试类
标记为测试方法
Assert.Throws 期望抛出异常
Constructor 在每个测试方法执行之前用于初始化的方法
IDisposable.Dispose 在每个测试方法执行之前用于结束的方法
IUseFixture<T> 在所有测试方法执行之前用于初始化的方法
跳过测试设置
用于指定被测试方法的最大执行时间(单位毫秒),若超过指定的标记时间,则被认为测试失败
设置任意的测试数据
, 理论上的数据驱动测试
断言比较
MSTest xUnit 备注
AreEqual Equal 相等比较
AreNotEqual NotEqual 不相等比较
AreNotSame NotSame 不相同比较
AreSame Same 相同比较
Contains (on CollectionAssert) Contains 包含比较
DoesNotContain (on CollectionAssert)
DoesNotContain 不包含比较
无
DoesNotThrow
不抛出异常
Fail 无 失败
Inconclusive 无
无 InRange 在范围内
无 IsAssignableFrom
无 Empty
IsFalse False
IsInstanceOfType IsType
IsNotInstanceOfType IsNotType
IsNotNull
NotNull
IsNull Null
IsTrue True
无 NotInRange 不在范围内
无 Throws 抛出异常
二、webAPI 创建xUnit单元测试
1、使用VSCode创建webAPI项目(命令 dotnet new webapi)
2、使用VSCode创建xunit项目(命令 dotnet new xunit)
3、xunit 项目添加对webAPI项目的引用
4、单元测试代码示例
using System;
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc;
using UnitTestCommonLib;
using UnitTestWebAPI.Controllers;
using Xunit;
namespace WebAPIUnitTest
{
public class ClerkUnitTest
{
CommonHelper commonHelper = new CommonHelper();
ClerkEntities clerkEntities = new ClerkEntities();
/// <summary>
/// test GetClerkList
/// </summary>
public void GetClerkList()
{
//create test data for CSB004
var clerkList = new List<CSB004>
{
new CSB004 { Ucode = "000000000001", ClkCode = "0000000001", ClkName = "clkname1", ClkGrpCode = "000001", DrawerNumber = "1", ClkIntCode = "000001", NbchkNumFrom = "000001", NbchkNumTo = "000009", Unusable = "0", CreateDateTime = DateTime.UtcNow.ToString ("yyyyMMddHHmmss"), UpdateDateTime = "" },
new CSB004 { Ucode = "000000000001", ClkCode = "0000000002", ClkName = "clkname2", ClkGrpCode = "000001", DrawerNumber = "1", ClkIntCode = "000001", NbchkNumFrom = "000001", NbchkNumTo = "000009", Unusable = "0", CreateDateTime = DateTime.UtcNow.ToString ("yyyyMMddHHmmss"), UpdateDateTime = "" },
new CSB004 { Ucode = "000000000002", ClkCode = "0000000002", ClkName = "clkname2", ClkGrpCode = "000001", DrawerNumber = "1", ClkIntCode = "000001", NbchkNumFrom = "000001", NbchkNumTo = "000009", Unusable = "0", CreateDateTime = DateTime.UtcNow.ToString ("yyyyMMddHHmmss"), UpdateDateTime = "" }
};
clerkEntities.csb004.AddRange(clerkList);
clerkEntities.SaveChanges();
//create test examples
var testcol = new GetClerkListController();
GetClerkListController.InputModel input = new GetClerkListController.InputModel();
// data exist
input.Ucode = "000000000001";
var actualValue = testcol.GetClerkList(input);
var actualModel = JsonHelper.JsonToEntity<CommonHelper.ReturnInfo>(actualValue);
//No data exist -No data in CSB004
input.Ucode = "000000000003";
var actualValue1 = testcol.GetClerkList(input);
// when ucode is empty
input.Ucode = "";
var actualValue2 = testcol.GetClerkList(input);
var actualModel2 = JsonHelper.JsonToEntity<CommonHelper.ReturnInfo>(actualValue2);
//clear test data
clerkEntities.csb004.RemoveRange(clerkList);
clerkEntities.SaveChanges();
//verify
Assert.NotNull(actualValue);
Assert.Equal(CommonHelper.ERROR_CORRECT, actualModel.errorcode);
Assert.Equal(commonHelper.ReturnCodeMessage(CommonHelper.ERROR_NORECORD), actualValue1);
Assert.Equal(CommonHelper.ERROR_REQUIRED, actualModel2.errorcode);
Assert.Equal("Ucode", actualModel2.maininfo);
}
5、注:测试类中的标注,防止同一个单元测试项目中,不同测试类中的不同测试方法在测试运行时,出现数据冲突,造成测试失败
页:
[1]