webAPI xUnit单元测试

通常我们在编码完成功能后,都需要进行单元测试,编写单元测试的好处在于有助于代码维护。

一、测试框架MSTest与xUnit比较

特性比较
MSTest xUnit 备注
[TestClass] 标记为测试类
[TestMethod] [Fact] 标记为测试方法
[ExpectedException] Assert.Throws 期望抛出异常
[TestInitialize] Constructor 在每个测试方法执行之前用于初始化的方法
[TestCleanup] IDisposable.Dispose 在每个测试方法执行之前用于结束的方法
[ClassInitialize] IUseFixture 在所有测试方法执行之前用于初始化的方法
[Ignore] [Fact(Skip="reason")] 跳过测试设置
[Timeout] [Fact(Timeout=n)] 用于指定被测试方法的最大执行时间(单位毫秒),若超过指定的标记时间,则被认为测试失败
[TestProperty] [Trait] 设置任意的测试数据
[DataSource] [Theory], [XxxData] 理论上的数据驱动测试
断言比较
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
{
	[Collection("Sequential")]
	public class ClerkUnitTest
	{
		CommonHelper commonHelper = new CommonHelper();
		ClerkEntities clerkEntities = new ClerkEntities();

		/// 
		/// test GetClerkList
		/// 
		[Fact]
		public void GetClerkList()
		{
			//create test data for CSB004
			var clerkList = new List
			{
				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(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(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、注:测试类中的[Collection("Sequential")]标注,防止同一个单元测试项目中,不同测试类中的不同测试方法在测试运行时,出现数据冲突,造成测试失败

你可能感兴趣的:(C#,.Net,Core,VSCode,webApi,xUnit)