Skip to content

Commit

Permalink
Support [only] mode (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
928PJY authored Apr 6, 2023
1 parent a708a51 commit 4e56fc4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
20 changes: 13 additions & 7 deletions src/yunit/TestAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,14 @@ void ExpandTest(TestData data)
public void RunTests(IEnumerable<TestCase> tests, IRunContext runContext, IFrameworkHandle frameworkHandle)
{
var testRuns = new ConcurrentBag<Task>();
Parallel.ForEach(tests, test => testRuns.Add(RunTest(frameworkHandle, test)));
var inOnlyMode = tests.Any(test => test.DisplayName.IndexOf("[only]", 0, StringComparison.OrdinalIgnoreCase) >= 0);
Parallel.ForEach(tests, test => testRuns.Add(RunTest(frameworkHandle, test, inOnlyMode)));
Task.WhenAll(testRuns).GetAwaiter().GetResult();
}

public void RunTests(IEnumerable<string> sources, IRunContext runContext, IFrameworkHandle frameworkHandle)
{
var testRuns = new ConcurrentBag<Task>();
var tests = new ConcurrentBag<TestCase>();

var filterExpression = runContext.GetTestCaseFilter(s_filteringProperties, null);

Expand All @@ -134,15 +135,15 @@ public void RunTests(IEnumerable<string> sources, IRunContext runContext, IFrame
{
if (filterExpression == null || filterExpression.MatchTestCase(test, name => GetPropertyValue(test, name)))
{
testRuns.Add(RunTest(frameworkHandle, test));
tests.Add(test);
}
},
message => frameworkHandle.SendMessage(TestMessageLevel.Warning, message));

Task.WhenAll(testRuns).GetAwaiter().GetResult();
RunTests(tests, runContext, frameworkHandle);
}

private async Task RunTest(ITestExecutionRecorder log, TestCase test)
private async Task RunTest(ITestExecutionRecorder log, TestCase test, bool inOnlyMode = false)
{
if (_canceled)
{
Expand All @@ -162,7 +163,7 @@ private async Task RunTest(ITestExecutionRecorder log, TestCase test)
result.StartTime = DateTime.UtcNow;

var timeout = test.GetPropertyValue<int>(s_timeoutProperty, 1000);
var runTest = RunTest(test);
var runTest = RunTest(test, inOnlyMode);

if (await Task.WhenAny(runTest, Task.Delay(timeout)) == runTest)
{
Expand Down Expand Up @@ -268,13 +269,18 @@ private static void DiscoverTests(ITestAttribute attribute, string sourcePath, A
Parallel.ForEach(files, file => attribute.DiscoverTests(Path.Combine(sourcePath, file), report));
}

private Task RunTest(TestCase test)
private Task RunTest(TestCase test, bool inOnlyMode = false)
{
if (test.DisplayName.IndexOf("[skip]", 0, StringComparison.OrdinalIgnoreCase) >= 0)
{
throw new TestSkippedException("Marked as [skip]");
}

if (inOnlyMode && test.DisplayName.IndexOf("[only]", 0, StringComparison.OrdinalIgnoreCase) < 0)
{
throw new TestSkippedException("Some other tests has been marked as [only]");
}

var (type, method) = GetMethodInfo(test.Source, test.FullyQualifiedName);
if (type is null || method is null)
{
Expand Down
17 changes: 11 additions & 6 deletions test/yunit.nuget.test/NuGetTest.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
using System;
using System.IO;
using System.IO;
using System.Threading.Tasks;

namespace Yunit.NuGetTest
{
public class NuGetTest
{
[MarkdownTest("~/test/yunit.nuget.test/**/*.md")]
[MarkdownTest("~/test/yunit.nuget.test/yunit.nuget.test.md")]
public void Foo(string filename)
{
File.WriteAllText(filename, "");
}

[MarkdownTest("~/test/yunit.nuget.test/**/*.md")]
[MarkdownTest("~/test/yunit.nuget.test/yunit-only.nuget.test.md")]
public void FooWithOnly(string filename)
{
File.WriteAllText(filename, "");
}

[MarkdownTest("~/test/yunit.nuget.test/yunit.nuget.test.md")]
public void SkipSync(string filename)
{
throw new TestSkippedException();
}

[MarkdownTest("~/test/yunit.nuget.test/**/*.md")]
[MarkdownTest("~/test/yunit.nuget.test/yunit.nuget.test.md")]
public async Task SkipAsync(TestData data, string filename)
{
await Task.Delay(1);
throw new TestSkippedException();
}

[MarkdownTest("~/test/yunit.nuget.test/**/*.md", ExpandTest = "ExpandTest")]
[MarkdownTest("~/test/yunit.nuget.test/yunit.nuget.test.md", ExpandTest = "ExpandTest")]
public void ExpandTestMethod(TestData data, string filename)
{
throw new TestSkippedException(data.Matrix);
Expand Down
11 changes: 11 additions & 0 deletions test/yunit.nuget.test/yunit-only.nuget.test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# [only] yunit.NuGetTest with only mode

``````yml
foo
``````

# This test should be skipped

``````yml
foo
``````

0 comments on commit 4e56fc4

Please sign in to comment.