Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move xUnit tests in new folder (#8356)
## Motivation I have a PR where there are many new xUnit tests. It would also be useful to create new xUnit tests for public APIs. The number of xUnit tests will increase and their ordering is required. ## PR Summary - Move C# xUnit tests in new folder. This allows to put new xUnit tests in directory structure in accordance with directory structure where cs files are. - Use an xUnit TestCaseOrderer attribute to sequentially process tests for `powershell.config.json`. - Update README.md - A race condition was fixed which allowed to run all XUnit tests in single batch job.
- Loading branch information
1 parent
cd8319a
commit a9f106d
Showing
20 changed files
with
122 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
|
||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] | ||
public class TestPriorityAttribute : Attribute | ||
{ | ||
public TestPriorityAttribute(int priority) | ||
{ | ||
Priority = priority; | ||
} | ||
|
||
public int Priority { get; private set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Xunit.Abstractions; | ||
using Xunit.Sdk; | ||
|
||
namespace TestOrder.TestCaseOrdering | ||
{ | ||
public class PriorityOrderer : ITestCaseOrderer | ||
{ | ||
public IEnumerable<TTestCase> OrderTestCases<TTestCase>(IEnumerable<TTestCase> testCases) where TTestCase : ITestCase | ||
{ | ||
var sortedMethods = new SortedDictionary<int, List<TTestCase>>(); | ||
|
||
foreach (TTestCase testCase in testCases) | ||
{ | ||
int priority = 0; | ||
|
||
foreach (IAttributeInfo attr in testCase.TestMethod.Method.GetCustomAttributes((typeof(TestPriorityAttribute).AssemblyQualifiedName))) | ||
priority = attr.GetNamedArgument<int>("Priority"); | ||
|
||
GetOrCreate(sortedMethods, priority).Add(testCase); | ||
} | ||
|
||
foreach (var list in sortedMethods.Keys.Select(priority => sortedMethods[priority])) | ||
{ | ||
list.Sort((x, y) => StringComparer.OrdinalIgnoreCase.Compare(x.TestMethod.Method.Name, y.TestMethod.Method.Name)); | ||
foreach (TTestCase testCase in list) | ||
yield return testCase; | ||
} | ||
} | ||
|
||
static TValue GetOrCreate<TKey, TValue>(IDictionary<TKey, TValue> dictionary, TKey key) where TValue : new() | ||
{ | ||
TValue result; | ||
|
||
if (dictionary.TryGetValue(key, out result)) return result; | ||
|
||
result = new TValue(); | ||
dictionary[key] = result; | ||
|
||
return result; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# xUnit Tests | ||
|
||
The folder contains xUnit tests for PowerShell Core project. | ||
|
||
## Running xUnit Tests | ||
|
||
Go to the top level of the PowerShell repository and run full set of tests: | ||
`Start-PSxUnit` inside a self-hosted copy of PowerShell. | ||
|
||
Go to the test project folder and run `dotnet test -c Release`. | ||
|
||
Use [`filter`](xunit-filter) parameter to run only needed tests: | ||
```powershell | ||
dotnet test -c Release --filter "FullyQualifiedName~UnitTest1 # Runs tests which have UnitTest1 in FullyQualifiedName | ||
dotnet test --filter Name~TestMethod1 # Runs tests whose name contains TestMethod1 | ||
``` | ||
|
||
## Creating xUnit Tests | ||
|
||
Keep the folder structure that is for Pester [../../test/powershell](../../test/powershell) and C# files [../../src](../../src). | ||
|
||
Use namespace names started with `PSTests`. | ||
```c# | ||
namespace PSTests.YourNameSpace | ||
{ | ||
} | ||
``` | ||
|
||
[xunit-filter]: https://docs.microsoft.com/en-us/dotnet/core/testing/selective-unit-tests |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters