Skip to content

Commit 4541b7e

Browse files
authored
Merge pull request #25 from dotnet-campus/t/lindexi/FixNotFoundName
修复找不到测试方法
2 parents 1affc9f + c5c58e1 commit 4541b7e

19 files changed

+553
-473
lines changed

.github/workflows/dotnet-build.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
runs-on: windows-latest
1515
steps:
1616
- name: Checkout
17-
uses: actions/checkout@v2
17+
uses: actions/checkout@v4
1818

1919
- name: Setup .NET
2020
uses: actions/setup-dotnet@v1
@@ -23,6 +23,8 @@ jobs:
2323
3.1.x
2424
5.0.x
2525
6.0.x
26+
8.0.x
27+
9.0.x
2628
2729
- name: Build
2830
run: dotnet build --configuration $env:Configuration

.github/workflows/nuget-push.yml

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ jobs:
1010
runs-on: windows-latest
1111
steps:
1212
- name: Checkout
13-
uses: actions/checkout@v2
13+
uses: actions/checkout@v4
14+
15+
- name: Install tool
16+
run: dotnet tool install -g dotnetCampus.TagToVersion
17+
18+
- name: Set tag to version
19+
run: dotnet TagToVersion -t ${{ github.ref }}
1420

1521
- name: Setup .NET
1622
uses: actions/setup-dotnet@v1
@@ -19,14 +25,10 @@ jobs:
1925
3.1.x
2026
5.0.x
2127
6.0.x
22-
23-
- name: Install tool
24-
run: dotnet tool install -g dotnetCampus.TagToVersion
25-
26-
- name: Set tag to version
27-
run: dotnet TagToVersion -t ${{ github.ref }}
28+
8.0.x
29+
9.0.x
2830
2931
- name: Pack
3032
run: dotnet build --configuration Release
3133
- name: Push
32-
run: dotnet nuget push .\bin\Release\*.nupkg --source https://api.nuget.org/v3/index.json --api-key ${{ secrets.NugetKey }} --skip-duplicate --no-symbols 1
34+
run: dotnet nuget push .\bin\Release\*.nupkg --source https://api.nuget.org/v3/index.json --api-key ${{ secrets.NugetKey }} --skip-duplicate

Directory.Build.props

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project>
1+
<Project>
22
<Import Project="build\Version.props" />
33
<PropertyGroup>
44
<PackageOutputPath>$(MSBuildThisFileDirectory)bin\$(Configuration)</PackageOutputPath>
@@ -8,12 +8,25 @@
88
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
99
<PackageTags>source;dotnet;nuget;msbuild</PackageTags>
1010
<PackageLicenseExpression>MIT</PackageLicenseExpression>
11+
<RepositoryUrl>https://github.com/dotnet-campus/CUnit.git</RepositoryUrl>
12+
<RepositoryType>git</RepositoryType>
13+
<PackageProjectUrl>https://github.com/dotnet-campus/CUnit</PackageProjectUrl>
14+
<Copyright>Copyright (c) 2018-$([System.DateTime]::Now.ToString(`yyyy`)) dotnet职业技术学院</Copyright>
15+
<PackageReadmeFile>README.md</PackageReadmeFile>
16+
<!-- 输出 pdb 文件 NuGet 包 -->
17+
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
18+
19+
<!-- 输出符号文件 -->
20+
<IncludeSymbols>true</IncludeSymbols>
21+
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
1122
</PropertyGroup>
1223

1324
<PropertyGroup>
1425
<PackageIcon>icon.png</PackageIcon>
1526
</PropertyGroup>
1627
<ItemGroup>
1728
<None Include="$(MSBuildThisFileDirectory)icon.png" Pack="true" PackagePath=""/>
29+
<None Include="..\..\README.md" Link="README.md" Pack="True" PackagePath="\"/>
1830
</ItemGroup>
31+
1932
</Project>

MSTest.Extensions.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "_SolutionItems", "_Solution
1616
.gitattributes = .gitattributes
1717
.gitignore = .gitignore
1818
build\code-style.ruleset = build\code-style.ruleset
19+
Directory.Build.props = Directory.Build.props
1920
LICENSE = LICENSE
2021
EndProjectSection
2122
EndProject

demo/dotnetCampus.UITest.WPF.Demo/dotnetCampus.UITest.WPF.Demo.csproj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222

2323
<ItemGroup>
2424
<PackageReference Include="Moq" Version="4.16.1" />
25-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
26-
<PackageReference Include="MSTest.TestAdapter" Version="2.2.8" />
27-
<PackageReference Include="MSTest.TestFramework" Version="2.2.8" />
25+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
2826
<PackageReference Include="coverlet.collector" Version="3.1.0" />
2927
</ItemGroup>
3028

src/MSTest.Extensions/Contracts/ContractTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Diagnostics.Contracts;
33
using System.Threading.Tasks;
44
using MSTest.Extensions.Core;
@@ -24,7 +24,7 @@ public static void Test([NotNull] this string contract, [NotNull] Action testCas
2424
if (testCase == null) throw new ArgumentNullException(nameof(testCase));
2525
Contract.EndContractBlock();
2626

27-
Method.Current.Add(new ContractTestCase(contract, testCase));
27+
Method.AddToCurrent(new ContractTestCase(contract, testCase));
2828
}
2929

3030
/// <summary>
@@ -39,7 +39,7 @@ public static void Test([NotNull] this string contract, [NotNull] Func<Task> tes
3939
if (testCase == null) throw new ArgumentNullException(nameof(testCase));
4040
Contract.EndContractBlock();
4141

42-
Method.Current.Add(new ContractTestCase(contract, testCase));
42+
Method.AddToCurrent(new ContractTestCase(contract, testCase));
4343
}
4444

4545
#endregion

src/MSTest.Extensions/Contracts/ContractTestCaseAttribute.cs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
3+
using MSTest.Extensions.Core;
4+
using MSTest.Extensions.Utils;
5+
16
using System;
27
using System.Collections.Generic;
38
using System.Diagnostics.CodeAnalysis;
49
using System.Diagnostics.Contracts;
510
using System.Linq;
611
using System.Reflection;
12+
using System.Security.Cryptography;
713
using System.Threading.Tasks;
8-
using Microsoft.VisualStudio.TestTools.UnitTesting;
9-
using MSTest.Extensions.Core;
10-
using MSTest.Extensions.Utils;
1114

1215
// ## How it works?
1316
//
@@ -80,12 +83,9 @@ public IEnumerable<object[]> GetData([NotNull] MethodInfo methodInfo)
8083
Contract.EndContractBlock();
8184

8285
// Collect all test cases from the target unit test method.
83-
Collect(methodInfo);
84-
85-
var cases = ContractTest.Method[methodInfo];
86-
VerifyContracts(cases);
86+
var cases = Collect(methodInfo);
8787

88-
return Enumerable.Range(0, cases.Count).Select(x => (object[]) null);
88+
return cases.Select(t => new object[] { t });
8989
}
9090

9191
/// <summary>
@@ -96,9 +96,13 @@ public IEnumerable<object[]> GetData([NotNull] MethodInfo methodInfo)
9696
/// <param name="data">The parameter list which was returned by <see cref="GetData"/>.</param>
9797
/// <returns>The display name of this test case.</returns>
9898
[NotNull]
99-
public string GetDisplayName([NotNull] MethodInfo methodInfo, [NotNull] object[] data)
99+
public string GetDisplayName([NotNull] MethodInfo methodInfo, [CanBeNull] object[] data)
100100
{
101-
return ContractTest.Method[methodInfo][_testCaseIndex++].DisplayName;
101+
Contract.Requires(methodInfo != null, "The method must not be null.");
102+
103+
return data is not null && data.Length > 0 && data[0] is ITestCase testCase
104+
? testCase.DisplayName
105+
: methodInfo.Name;
102106
}
103107

104108
#endregion
@@ -107,14 +111,18 @@ public string GetDisplayName([NotNull] MethodInfo methodInfo, [NotNull] object[]
107111

108112
[SuppressMessage("ReSharper", "AssignNullToNotNullAttribute")]
109113
[SuppressMessage("ReSharper", "PossibleNullReferenceException")]
110-
private static void Collect([NotNull] MethodInfo methodInfo)
114+
[NotNull]
115+
private static IReadOnlyList<ITestCase> Collect([NotNull] MethodInfo methodInfo)
111116
{
112117
var type = methodInfo.DeclaringType;
113118
Contract.Requires(type != null,
114119
"The method must be declared in a type. If this exception happened, there might be a bug in MSTest v2.");
115120

116121
var testInstance = Activator.CreateInstance(type);
117-
var testCaseList = ContractTest.Method[methodInfo];
122+
123+
124+
var testCaseList = new List<ITestCase>();
125+
ContractTest.Method.SetCurrentCollection(methodInfo, testCaseList);
118126
try
119127
{
120128
// Invoke target test method to collect all test cases.
@@ -155,13 +163,16 @@ Try to call Test extension method to collect one.
155163
156164
If you only need to write a normal test method, use `TestMethodAttribute` instead of `ContractTestCaseAttribute`."));
157165
}
166+
167+
VerifyContracts(testCaseList);
168+
return testCaseList;
158169
}
159170

160171
/// <summary>
161172
/// Find out the test cases which have the same contract string, add a special exception to it.
162173
/// </summary>
163174
/// <param name="cases">The test cases of a single test method.</param>
164-
private void VerifyContracts([NotNull] IList<ITestCase> cases)
175+
private static void VerifyContracts([NotNull] List<ITestCase> cases)
165176
{
166177
var caseContractSet = new HashSet<string>();
167178
var duplicatedCases = new HashSet<ITestCase>();
@@ -200,13 +211,6 @@ 1. Please check whether you have created two test cases which have the same cont
200211

201212
#endregion
202213

203-
/// <summary>
204-
/// Gets or increment the current test case index.
205-
/// <see cref="Execute"/> and <see cref="GetDisplayName"/> should increment it separately
206-
/// because they are not in the same instance.
207-
/// </summary>
208-
private int _testCaseIndex;
209-
210214
/// <summary>
211215
/// the proxy of ITestMethod(TestMethodInfo in fact)
212216
/// overwrite the invoke method

src/MSTest.Extensions/Contracts/ContractTestContext.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Diagnostics.Contracts;
33
using System.Runtime.CompilerServices;
44
using System.Threading.Tasks;
@@ -97,7 +97,7 @@ public ContractTestContext<T> WithArguments([CanBeNullWhenTIsSingle] params T[]
9797
}
9898

9999
// Add an argument test case to the test case list.
100-
ContractTest.Method.Current.Add(new ContractTestCase(contract, () => _testCase(t)));
100+
ContractTest.Method.AddToCurrent(new ContractTestCase(contract, () => _testCase(t)));
101101
}
102102

103103
return this;
@@ -120,7 +120,7 @@ private string ForT<TInput>([CanBeNull] TInput value)
120120
[NotNull, PublicAPI]
121121
public ContractTestContext<T> WithArguments(T t)
122122
{
123-
ContractTest.Method.Current.Add(new ContractTestCase(
123+
ContractTest.Method.AddToCurrent(new ContractTestCase(
124124
string.Format(_contract, t), () => _testCase(t)));
125125
return this;
126126
}

src/MSTest.Extensions/Core/ContractTestCase.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Diagnostics;
33
using System.Diagnostics.Contracts;
44
using System.Globalization;
@@ -129,8 +129,8 @@ internal async Task<TestResult> ExecuteAsync()
129129
if (ContractTestConfiguration.MustSTAThread &&
130130
Thread.CurrentThread.GetApartmentState() != ApartmentState.STA)
131131
{
132-
// 强行要求 STA 且当前运行非 STA 线程的情况,就需要进行切换线程
133-
// 这里是单元测试,就可以无视性能问题哈
132+
// 强行要求 STA 且当前运行非 STA 线程的情况,就需要进行切换线程
133+
// 这里是单元测试,就可以无视性能问题哈
134134
var thread = new Thread(() =>
135135
{
136136
try
@@ -139,7 +139,7 @@ internal async Task<TestResult> ExecuteAsync()
139139
}
140140
catch (Exception e)
141141
{
142-
// 不能抛到后台线程去
142+
// 不能抛到后台线程去
143143
exception = e;
144144
}
145145
});
@@ -155,7 +155,7 @@ internal async Task<TestResult> ExecuteAsync()
155155
}
156156
catch (AggregateException ex)
157157
{
158-
// If this test case is an async method, extract the inner excetion.
158+
// If this test case is an async method, extract the inner exception.
159159
exception = ex.InnerExceptions.Count == 1 ? ex.InnerException : ex;
160160
}
161161
catch (Exception ex)

0 commit comments

Comments
 (0)