-
-
Notifications
You must be signed in to change notification settings - Fork 130
feat: add JUnit reporter and command provider for test results output #3987
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
dc7ff91
feat: add JUnit reporter and command provider for test results output
thomhurst c612ac8
refactor: simplify JUnit reporter's disable checks and clean up tests
thomhurst adfd92c
refactor: replace EnvironmentVariableCache with direct Environment ca…
thomhurst File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,120 @@ | ||
| using Microsoft.Testing.Platform.Extensions; | ||
| using TUnit.Core; | ||
| using TUnit.Engine.Reporters; | ||
|
|
||
| namespace TUnit.Engine.Tests; | ||
|
|
||
| [NotInParallel] | ||
| public class JUnitReporterTests | ||
| { | ||
| private sealed class MockExtension : IExtension | ||
| { | ||
| public string Uid => "MockExtension"; | ||
| public string DisplayName => "Mock"; | ||
| public string Version => "1.0.0"; | ||
| public string Description => "Mock Extension"; | ||
| public Task<bool> IsEnabledAsync() => Task.FromResult(true); | ||
| } | ||
|
|
||
| [After(Test)] | ||
| public void Cleanup() | ||
| { | ||
| // Clean up environment variables after each test | ||
| Environment.SetEnvironmentVariable("TUNIT_DISABLE_JUNIT_REPORTER", null); | ||
| Environment.SetEnvironmentVariable("TUNIT_ENABLE_JUNIT_REPORTER", null); | ||
| Environment.SetEnvironmentVariable("GITLAB_CI", null); | ||
| Environment.SetEnvironmentVariable("CI_SERVER", null); | ||
| Environment.SetEnvironmentVariable("JUNIT_XML_OUTPUT_PATH", null); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task IsEnabledAsync_Should_Return_False_When_TUNIT_DISABLE_JUNIT_REPORTER_Is_Set() | ||
| { | ||
| // Arrange | ||
| Environment.SetEnvironmentVariable("TUNIT_DISABLE_JUNIT_REPORTER", "true"); | ||
| Environment.SetEnvironmentVariable("GITLAB_CI", "true"); // Even with GitLab CI, should be disabled | ||
| var extension = new MockExtension(); | ||
| var reporter = new JUnitReporter(extension); | ||
|
|
||
| // Act | ||
| var isEnabled = await reporter.IsEnabledAsync(); | ||
|
|
||
| // Assert | ||
| await Assert.That(isEnabled).IsFalse(); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task IsEnabledAsync_Should_Return_True_When_GITLAB_CI_Is_Set() | ||
| { | ||
| // Arrange | ||
| Environment.SetEnvironmentVariable("GITLAB_CI", "true"); | ||
| var extension = new MockExtension(); | ||
| var reporter = new JUnitReporter(extension); | ||
|
|
||
| // Act | ||
| var isEnabled = await reporter.IsEnabledAsync(); | ||
|
|
||
| // Assert | ||
| await Assert.That(isEnabled).IsTrue(); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task IsEnabledAsync_Should_Return_True_When_CI_SERVER_Is_Set() | ||
| { | ||
| // Arrange | ||
| Environment.SetEnvironmentVariable("CI_SERVER", "yes"); | ||
| var extension = new MockExtension(); | ||
| var reporter = new JUnitReporter(extension); | ||
|
|
||
| // Act | ||
| var isEnabled = await reporter.IsEnabledAsync(); | ||
|
|
||
| // Assert | ||
| await Assert.That(isEnabled).IsTrue(); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task IsEnabledAsync_Should_Return_True_When_TUNIT_ENABLE_JUNIT_REPORTER_Is_Set() | ||
| { | ||
| // Arrange | ||
| Environment.SetEnvironmentVariable("TUNIT_ENABLE_JUNIT_REPORTER", "true"); | ||
| var extension = new MockExtension(); | ||
| var reporter = new JUnitReporter(extension); | ||
|
|
||
| // Act | ||
| var isEnabled = await reporter.IsEnabledAsync(); | ||
|
|
||
| // Assert | ||
| await Assert.That(isEnabled).IsTrue(); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task IsEnabledAsync_Should_Return_False_When_No_Environment_Variables_Are_Set() | ||
| { | ||
| // Arrange | ||
| var extension = new MockExtension(); | ||
| var reporter = new JUnitReporter(extension); | ||
|
|
||
| // Act | ||
| var isEnabled = await reporter.IsEnabledAsync(); | ||
|
|
||
| // Assert | ||
| await Assert.That(isEnabled).IsFalse(); | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task IsEnabledAsync_Should_Prefer_Disable_Over_Enable() | ||
| { | ||
| // Arrange | ||
| Environment.SetEnvironmentVariable("TUNIT_DISABLE_JUNIT_REPORTER", "true"); | ||
| Environment.SetEnvironmentVariable("TUNIT_ENABLE_JUNIT_REPORTER", "true"); | ||
| var extension = new MockExtension(); | ||
| var reporter = new JUnitReporter(extension); | ||
|
|
||
| // Act | ||
| var isEnabled = await reporter.IsEnabledAsync(); | ||
|
|
||
| // Assert | ||
| await Assert.That(isEnabled).IsFalse(); | ||
| } | ||
| } | ||
45 changes: 45 additions & 0 deletions
45
TUnit.Engine/CommandLineProviders/JUnitReporterCommandProvider.cs
This file contains hidden or 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,45 @@ | ||
| using Microsoft.Testing.Platform.CommandLine; | ||
| using Microsoft.Testing.Platform.Extensions; | ||
| using Microsoft.Testing.Platform.Extensions.CommandLine; | ||
|
|
||
| namespace TUnit.Engine.CommandLineProviders; | ||
|
|
||
| internal class JUnitReporterCommandProvider(IExtension extension) : ICommandLineOptionsProvider | ||
| { | ||
| public const string JUnitOutputPathOption = "junit-output-path"; | ||
|
|
||
| public Task<bool> IsEnabledAsync() => extension.IsEnabledAsync(); | ||
|
|
||
| public string Uid => extension.Uid; | ||
|
|
||
| public string Version => extension.Version; | ||
|
|
||
| public string DisplayName => extension.DisplayName; | ||
|
|
||
| public string Description => extension.Description; | ||
|
|
||
| public IReadOnlyCollection<CommandLineOption> GetCommandLineOptions() | ||
| { | ||
| return | ||
| [ | ||
| new CommandLineOption( | ||
| JUnitOutputPathOption, | ||
| "Path to output JUnit XML file (default: TestResults/{AssemblyName}-junit.xml)", | ||
| ArgumentArity.ExactlyOne, | ||
| false) | ||
| ]; | ||
| } | ||
|
|
||
| public Task<ValidationResult> ValidateOptionArgumentsAsync( | ||
| CommandLineOption commandOption, | ||
| string[] arguments) | ||
| { | ||
| return ValidationResult.ValidTask; | ||
| } | ||
|
|
||
| public Task<ValidationResult> ValidateCommandLineOptionsAsync( | ||
| ICommandLineOptions commandLineOptions) | ||
| { | ||
| return ValidationResult.ValidTask; | ||
| } | ||
| } |
This file contains hidden or 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.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tests will not work as expected because they set environment variables directly, but the
JUnitReporterusesEnvironmentVariableCache.Get()which caches environment variables on first access. Once the cache is initialized, setting environment variables withEnvironment.SetEnvironmentVariable()won't affect the cached values.To fix this, you need to either:
EnvironmentVariableCacheand call it in theCleanup()method