在 xunit 中不需要标记测试类,所有 public 的类似都可以作为测试类,测试方法需要使用 Fact 或者 Theory 注解来标注方法,来看一个基本的使用示例:
首先准备了几个要测试的方法:
internal class Helper{ public static int Add(int x, int y) { return x + y; } public static void ArgumentExceptionTest() => throw new ArgumentException(); public static void ArgumentNullExceptionTest() => throw new ArgumentNullException();}
测试代码:
public class BasicTest{ [Fact] public void AddTest() { Assert.Equal(4, Helper.Add(2, 2)); Assert.NotEqual(3, Helper.Add(2, 2)); } [Theory] [InlineData(1, 2)] [InlineData(2, 2)] public void AddTestWithTestData(int num1, int num2) { Assert.Equal(num1 + num2, Helper.Add(num1, num2)); }}
/// /// Apply this attribute to your test method to replace the/// and/// with another culture./// [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]public class UseCultureAttribute : BeforeAfterTestAttribute{ private readonly Lazy _culture; private readonly Lazy _uiCulture; private CultureInfo _originalCulture; private CultureInfo _originalUiCulture; /// /// Replaces the culture and UI culture of the current thread with /// /// /// The name of the culture. /// /// /// This constructor overload uses for both /// and . /// /// public UseCultureAttribute(string culture) : this(culture, culture) { } /// /// Replaces the culture and UI culture of the current thread with /// and /// /// The name of the culture. /// The name of the UI culture. public UseCultureAttribute(string culture, string uiCulture) { _culture = new Lazy (() => new CultureInfo(culture, false)); _uiCulture = new Lazy (() => new CultureInfo(uiCulture, false)); } /// /// Gets the culture. /// public CultureInfo Culture { get { return _culture.Value; } } /// /// Gets the UI culture. /// public CultureInfo UICulture { get { return _uiCulture.Value; } } /// /// Stores the current /// and /// and replaces them with the new cultures defined in the constructor. /// /// The method under test public override void Before(MethodInfo methodUnderTest) { _originalCulture = Thread.CurrentThread.CurrentCulture; _originalUiCulture = Thread.CurrentThread.CurrentUICulture; Thread.CurrentThread.CurrentCulture = Culture; Thread.CurrentThread.CurrentUICulture = UICulture; CultureInfo.CurrentCulture.ClearCachedData(); CultureInfo.CurrentUICulture.ClearCachedData(); } /// /// Restores the original and /// to /// /// The method under test public override void After(MethodInfo methodUnderTest) { Thread.CurrentThread.CurrentCulture = _originalCulture; Thread.CurrentThread.CurrentUICulture = _originalUiCulture; CultureInfo.CurrentCulture.ClearCachedData(); CultureInfo.CurrentUICulture.ClearCachedData(); }}
这里实现了一个设置测试用例运行过程中 Thread.CurrentThread.Culture 的属性,测试结束后恢复原始的属性值,可以用作于 Class 也可以用在测试方法中,使用示例如下:
[UseCulture("en-US", "zh-CN")]public class FilterTest{ [Fact] [UseCulture("en-US")] public void CultureTest() { Assert.Equal("en-US", Thread.CurrentThread.CurrentCulture.Name); } [Fact] [UseCulture("zh-CN")] public void CultureTest2() { Assert.Equal("zh-CN", Thread.CurrentThread.CurrentCulture.Name); } [Fact] public void CultureTest3() { Assert.Equal("en-US", Thread.CurrentThread.CurrentCulture.Name); Assert.Equal("zh-CN", Thread.CurrentThread.CurrentUICulture.Name); }}