Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support [only] mode #114

Merged
merged 1 commit into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]");
yufeih marked this conversation as resolved.
Show resolved Hide resolved
}

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
``````