You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
2.3 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
using Xunit.Extensions;
using Xunit.Sdk;
using xUnitStudy.Model;
namespace xUnitStudy.WebApi.Test
{
/// <summary>
/// 测试用例级别的共享
/// </summary>
public class UseFixtureTest:IDisposable
{
/* 学习
测试时无论是针对一些比较耗费资源的对象亦或是为了支持Test case预设数据的能力我们都需要有一些初始化或是清理相关的动作。
在每个测试用例执行前和执行后,可能需要执行相同的准备和清理工作。
本例方法1放在构造函数和IDisposable接口的实现方法Dispose()中
执行流程:
用例b创建测试类实例 --> 构造函数 --> 用例1 --> Dispose()方法 --> 结束测试类实例 --> 完成
用例a创建测试类实例 --> 构造函数 --> 用例2 --> Dispose()方法 --> 结束测试类实例 --> 完成
用例x创建测试类实例 --> 构造函数 --> 用例2 --> Dispose()方法 --> 结束测试类实例 --> 完成
用例m创建测试类实例 --> 构造函数 --> 用例2 --> Dispose()方法 --> 结束测试类实例 --> 完成
........
所有用例完成
注意用例执行的先后顺序由xUnit框架指定且先后顺序不固定。尤其是并行执行时执行顺序和返回顺序更不确定。
所以,各单元测试是完全独立的,相互之间不能存在任何依赖,特别是执行顺序、执行结果、共享数据。
*/
object sharedData;
public UseFixtureTest()
{
//测试用例执行前做准备
sharedData = "sharedate";
}
[Fact]
public void Case2_Test()
{
Assert.Equal("sharedate", sharedData.ToString());
}
[Fact]
public void Case1_Test()
{
Assert.Equal("sharedate", sharedData.ToString());
}
public void Dispose()
{
//测试用例执行后做清理
sharedData = null;
}
}
}