-
Notifications
You must be signed in to change notification settings - Fork 67
Fail Android runs when pulled test results report failed tests #1665
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
4 commits
Select commit
Hold shift + click to select a range
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
72 changes: 72 additions & 0 deletions
72
src/Microsoft.DotNet.XHarness.Android/TestResultsAnalyzer.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,72 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Xml.Linq; | ||
|
|
||
| namespace Microsoft.DotNet.XHarness.Android; | ||
|
|
||
| /// <summary> | ||
| /// Reads test result files (XML) produced by test applications and tells whether they contain failed tests. | ||
| /// This is needed because some applications report a zero instrumentation exit code even when tests failed. | ||
| /// </summary> | ||
| public static class TestResultsAnalyzer | ||
| { | ||
| /// <summary> | ||
| /// Returns the number of failed tests found in given test results file. | ||
| /// Returns null when the file is missing or its format is not recognized (in which case we can't tell). | ||
| /// </summary> | ||
| public static int? GetFailedTestCount(string resultsFilePath) | ||
| { | ||
| if (string.IsNullOrEmpty(resultsFilePath) || !File.Exists(resultsFilePath)) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| XDocument document; | ||
| try | ||
| { | ||
| document = XDocument.Load(resultsFilePath); | ||
| } | ||
| catch (Exception) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| XElement? root = document.Root; | ||
| if (root == null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| switch (root.Name.LocalName) | ||
| { | ||
| // xUnit v2 format (the default of the XHarness test runners) | ||
| case "assemblies": | ||
| case "assembly": | ||
| var assemblies = root.Name.LocalName == "assembly" | ||
| ? new[] { root } | ||
| : root.Elements().Where(e => e.Name.LocalName == "assembly").ToArray(); | ||
|
|
||
| // An empty <assemblies /> element is a valid result file with no failures | ||
| return assemblies.Sum(assembly => GetIntAttribute(assembly, "failed") + GetIntAttribute(assembly, "errors")); | ||
|
|
||
| // NUnit v2 format | ||
| case "test-results": | ||
| return GetIntAttribute(root, "failures") + GetIntAttribute(root, "errors"); | ||
|
|
||
| // NUnit v3 format | ||
| case "test-run": | ||
| return GetIntAttribute(root, "failed"); | ||
|
|
||
| default: | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| private static int GetIntAttribute(XElement element, string attributeName) | ||
| => int.TryParse(element.Attribute(attributeName)?.Value, out int value) ? value : 0; | ||
| } |
114 changes: 114 additions & 0 deletions
114
tests/Microsoft.DotNet.XHarness.Android.Tests/TestResultsAnalyzerTests.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,114 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| #nullable enable | ||
|
|
||
| using System; | ||
| using System.IO; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.DotNet.XHarness.Android.Tests; | ||
|
|
||
| public class TestResultsAnalyzerTests : IDisposable | ||
| { | ||
| private readonly string _tempDirectory; | ||
|
|
||
| public TestResultsAnalyzerTests() | ||
| { | ||
| _tempDirectory = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); | ||
| Directory.CreateDirectory(_tempDirectory); | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| if (Directory.Exists(_tempDirectory)) | ||
| { | ||
| Directory.Delete(_tempDirectory, true); | ||
| } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void XUnitResultsWithFailuresAreDetected() | ||
| { | ||
| var path = WriteResultsFile( | ||
| @"<assemblies> | ||
| <assembly name=""System.Numerics.Vectors.Tests.dll"" total=""1194"" passed=""1191"" failed=""3"" skipped=""0"" errors=""0"" /> | ||
| </assemblies>"); | ||
|
|
||
| Assert.Equal(3, TestResultsAnalyzer.GetFailedTestCount(path)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void XUnitResultsWithoutFailuresAreDetected() | ||
| { | ||
| var path = WriteResultsFile( | ||
| @"<assemblies> | ||
| <assembly name=""System.Buffers.Tests.dll"" total=""100"" passed=""100"" failed=""0"" skipped=""0"" errors=""0"" /> | ||
| </assemblies>"); | ||
|
|
||
| Assert.Equal(0, TestResultsAnalyzer.GetFailedTestCount(path)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void XUnitErrorsAreCountedAsFailures() | ||
| { | ||
| var path = WriteResultsFile( | ||
| @"<assembly name=""Some.Tests.dll"" total=""10"" passed=""9"" failed=""0"" skipped=""0"" errors=""1"" />"); | ||
|
|
||
| Assert.Equal(1, TestResultsAnalyzer.GetFailedTestCount(path)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void NUnitV2ResultsWithFailuresAreDetected() | ||
| { | ||
| var path = WriteResultsFile( | ||
| @"<test-results name=""Some.Tests"" total=""10"" errors=""1"" failures=""2"" not-run=""0"" />"); | ||
|
|
||
| Assert.Equal(3, TestResultsAnalyzer.GetFailedTestCount(path)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void NUnitV3ResultsWithFailuresAreDetected() | ||
| { | ||
| var path = WriteResultsFile( | ||
| @"<test-run id=""2"" testcasecount=""10"" result=""Failed"" total=""10"" passed=""8"" failed=""2"" />"); | ||
|
|
||
| Assert.Equal(2, TestResultsAnalyzer.GetFailedTestCount(path)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void EmptyXUnitResultsMeanNoFailures() | ||
| { | ||
| var path = WriteResultsFile(@"<assemblies />"); | ||
|
|
||
| Assert.Equal(0, TestResultsAnalyzer.GetFailedTestCount(path)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void UnknownFormatIsNotEvaluated() | ||
| { | ||
| var path = WriteResultsFile(@"<some-other-format failed=""3"" />"); | ||
|
|
||
| Assert.Null(TestResultsAnalyzer.GetFailedTestCount(path)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void MalformedFileIsNotEvaluated() | ||
| { | ||
| var path = WriteResultsFile("this is not XML"); | ||
|
|
||
| Assert.Null(TestResultsAnalyzer.GetFailedTestCount(path)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void MissingFileIsNotEvaluated() | ||
| => Assert.Null(TestResultsAnalyzer.GetFailedTestCount(Path.Combine(_tempDirectory, "does-not-exist.xml"))); | ||
|
|
||
| private string WriteResultsFile(string content) | ||
| { | ||
| var path = Path.Combine(_tempDirectory, "testResults.xml"); | ||
| File.WriteAllText(path, content); | ||
| return path; | ||
| } | ||
| } |
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
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.
Fyi. This will take the app from
test-apk/x86_64but the code before usedtest-apk/x86. It looks like the test is passing now so it appears that the other APK was probably stale and should be removed from the storage.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.
I uploaded a new rebuilt version there - but yes, eventually we need to clean it up. Unfortunately I don't have access to list all the blobs which are there... :-)
I'll talk to the person who does.