测试类共享数据

develop
bicijinlian
parent 5aab2368ca
commit 0ca4b3541d

@ -46,6 +46,7 @@
<Compile Include="Student.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="xUnitStudy\AssertEqualDemo.cs" />
<Compile Include="xUnitStudy\FixtureDemo.cs" />
<Compile Include="xUnitStudy\IPerson.cs" />
<Compile Include="xUnitStudy\Person.cs" />
<Compile Include="xUnitStudy\Teacher.cs" />

@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace xUnitStudy.Model
{
/// <summary>
/// 测试方法共享对象实例
/// </summary>
public class FixtureDemo : IDisposable
{
public int CallTimes { get; private set; }
public List<Person> Persons;
public FixtureDemo()
{
Persons = new List<Person>()
{
new Person(){ Id=1, FirstName="first1",LastName="last1" },
new Person(){ Id=2, FirstName="first2",LastName="last2" },
new Person(){ Id=3, FirstName="first3",LastName="last3" },
};
}
public (bool result, Person person) AddPerson(Person person)
{
CallTimes += 1;
var exist = Persons.FirstOrDefault(p => p.Id == person.Id);
if (exist == null)
{
Persons.Add(person);
return ValueTuple.Create(true, person);
}
else
{
return ValueTuple.Create(false, person);
}
}
public (bool result, Person person) RemovePerson(Person person)
{
CallTimes += 1;
var exist = Persons.FirstOrDefault(p => p.Id == person.Id);
if (exist == null)
{
return ValueTuple.Create(false, person);
}
else
{
Persons.Remove(exist);
return ValueTuple.Create(true, exist);
}
}
public void Dispose()
{
Persons = null;
}
}
}

@ -0,0 +1,34 @@
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;
namespace xUnitStudy.WebApi.Test
{
/// <summary>
/// 输出窗口的测试选项,显示测试相关信息
/// </summary>
public class TestCaseOrdererTest: ITestCaseOrderer
{
private readonly IMessageSink diagnosticMessageSink;
public TestCaseOrdererTest(IMessageSink diagnosticMessageSink)
{
this.diagnosticMessageSink = diagnosticMessageSink;
}
public IEnumerable<TTestCase> OrderTestCases<TTestCase>(IEnumerable<TTestCase> testCases) where TTestCase : ITestCase
{
var result = testCases.ToList(); // Run them in discovery order
var message = new DiagnosticMessage("Ordered {0} test cases", result.Count);
diagnosticMessageSink.OnMessage(message);
return result;
}
}
}

@ -4,9 +4,87 @@ 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
{
public class UseXUnitIClassFixtureTest
/// <summary>
/// 类内所有测试,共享一个实例
/// 注意:因为不能保证各个单元测试的执行先后顺序或者特别是并行单元测试时,
/// 很容因共享类的并发操作导致单元测试失败。共享须谨慎。
/// 适合读,不适合写
/// </summary>
public class UseXUnitIClassFixtureTest:IClassFixture<FixtureDemo>, IDisposable
{
FixtureDemo fixtureDemo;
public UseXUnitIClassFixtureTest(FixtureDemo fixture)
{
this.fixtureDemo = fixture;
}
[Fact]
public void NotNull_Test()
{
Assert.NotNull(fixtureDemo);
}
[Fact]
public void GetPersons_Test()
{
var persons = fixtureDemo.Persons;
//因为共享所以数量不一定是初始值3
//Assert.Equal(3,fixtureDemo.Persons.Count);
}
[Fact]
public void AddPerson_Test()
{
var callTimes_start = fixtureDemo.CallTimes;
var person1 = new Person() { Id=1,FirstName="first",LastName="last"};
var person2 = new Person() {Id=200};
//因为共享在不能保证单元测试的执行先后顺充时或者并行执行单元测试时可能id=1的项已被删除也可能没被删除
//所以result1可能失败也可能成功
var result1 = fixtureDemo.AddPerson(person1);
var result2 = fixtureDemo.AddPerson(person2);
//Assert.False(result1.result); 执行成功与否,取决于添加和删除单元测试的执行顺序。因为执行顺序不能确定,所以结果不定。
Assert.True(result2.result);
Assert.Equal(4, fixtureDemo.Persons.Count);
Assert.Contains(person2, fixtureDemo.Persons);
var callTimes_end = fixtureDemo.CallTimes;
Assert.Equal(2, callTimes_end - callTimes_start);
}
[Fact]
public void RemovePerson_Test()
{
var person1 = new Person() { Id = 1, FirstName = "first", LastName = "last" };
var person2 = new Person() { Id = 100 };
var result1 = fixtureDemo.RemovePerson(person1);
var result2 = fixtureDemo.RemovePerson(person2);
Assert.True(result1.result);
Assert.False(result2.result);
Assert.DoesNotContain(person1, fixtureDemo.Persons);
Assert.DoesNotContain(person2, fixtureDemo.Persons);
}
public void Dispose()
{
}
}
}

@ -3,10 +3,25 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
namespace xUnitStudy.WebApi.Test
{
public class UseXUnitOutputTest
{
private readonly ITestOutputHelper output;
public UseXUnitOutputTest(ITestOutputHelper output)
{
this.output = output;
}
[Fact]
public void Test()
{
Assert.True(true, "输出测试");
output.WriteLine("我是输出测试信息哦。");
}
}
}

@ -1,5 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="xunit.diagnosticMessages" value="true"/>
</appSettings>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>

@ -84,6 +84,7 @@
<Compile Include="AutoFac\AutofacStudy.cs" />
<Compile Include="StudentBll\StudentBllTest.cs" />
<Compile Include="StudentDal\StudentDalTest.cs" />
<Compile Include="TestCaseOrdererTest.cs" />
<Compile Include="UseXUnitAssertTest.cs" />
<Compile Include="UseXUnitIClassFixtureTest.cs" />
<Compile Include="UseXUnitICollectionFixtureTest.cs" />

Loading…
Cancel
Save