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

[tests] option to include/exclude categories from Test APKs #1024

Merged
merged 1 commit into from
Nov 14, 2017

Conversation

jonathanpeppers
Copy link
Member

@jonathanpeppers jonathanpeppers commented Nov 13, 2017

In some QA needs to run tests on devices without a network connection,
so an option to disable tests that rely on the network in Mono.Android-Tests
is needed.

NUnit has the option to include or exclude tests based on the
CategoryAttribute. Various tests were decorated with
[Category("InetAccess")] that appeared to use the internet. To verify
this works, I ran the tests with my internet disabled. The test run for
Mono.Android-Tests went down from 106 to 84 tests, with no failures.

To exclude a category, on Windows:

    msbuild Xamarin.Android.sln /t:RunApkTests /p:ExcludeCategories=InetAccess

On MacOS/Linux:

    make run-apk-tests EXCLUDECATEGORIES=InetAccess

If you want to specify multiple categories, use : (colon) to delimit
each category. This delimiter is used for various values throughout
Xamarin.Android's build because it works well from the command line for both
MSBuild and make.

I also added the option specify Include as well, but it isn't particularly
useful for this case.

@dnfclas
Copy link

dnfclas commented Nov 13, 2017

@jonathanpeppers,
Thanks for having already signed the Contribution License Agreement. Your agreement was validated by .NET Foundation. We will now review your pull request.
Thanks,
.NET Foundation Pull Request Bot

@jonathanpeppers
Copy link
Member Author

To match Mono, I need to change the category to InetAccess: mono/mono@9b4b6d6

@jonpryor
Copy link
Member

To exclude a category, on Windows:

msbuild Xamarin.Android.sln /t:RunApkTests /p:Exclude=InetAccess

I'm not sure $(Include) and $(Exclude) are the "best" property names here; it's potentially ambiguous. It's a verb without a noun.

I'd suggest $(IncludeCategories) and $(ExcludeCategories).

Additionally, please update Documentation/DevelopmentTips.md with this documentation.

Finally, as this updates e.g. TestSuiteInstrumentation.GetExcludedCategories(), this PR should probably update all overrides so that they'll work as expected. For example, within Xamarin.Android.Bcl-Tests:

		protected override IEnumerable<string> GetExcludedCategories ()
		{
			return App.GetExcludedCategories ();
		}

We'll want Xamarin.Android.Bcl-Tests to follow suit, even if there are currently no tests that will make use of this functionality. When there are such tests, we'll get very confused if the $(ExcludeCategories) property doesn't work on Xamarin.Android.Bcl-Tests, so we should fix that now, while we're thinking of it:

		protected override IEnumerable<string> GetExcludedCategories ()
		{
			var external = base.GetExcludedCategories ();
			return external != null
				? external.Concat (App.GetExcludedCategories ())
				: App.GetExternalCategories ();
		}

Writing the above makes me think that we should also update the semantics of GetExcludedCategories() and GetIncludedCategories() so that they never return null (because ugh).

}

protected virtual IEnumerable <string> GetExcludedCategories ()
{
return null;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of returning null, these will be the equivalent of yield break

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TestSuiteActivity should also be updated to not return null.

@@ -381,7 +381,7 @@ internal void AddTestFilters (IEnumerable<string> included, IEnumerable<string>
static void ChainCategoryFilter (IEnumerable <string> categories, bool negate, ref TestFilter chain)
{
bool gotCategories = false;
if (categories != null) {
if (categories != null && categories.Any ()) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to check Any(), because an empty IEnumerable should also be skipped

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should arguably be skipped, but it means that categories will be enumerated multiple times, which is potentially problematic.

It's probably better to not use the .Any(), and have an alternate check.

@@ -152,12 +152,17 @@

<Target Name="RunTestApks"
Condition=" '@(TestApk)' != '' ">
<PropertyGroup>
<_InstrumentationArguments Condition=" '$(IncludeCategories)' != '' ">include=$(IncludeCategories)</_InstrumentationArguments>
<_InstrumentationArguments Condition=" '$(ExcludeCategories)' != '' ">exclude=$(ExcludeCategories)</_InstrumentationArguments>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This construct means that you can't use both $(IncludeCategories) and $(ExcludeCategories) at the same time:

msbuild Xamarin.Android.sln /t:RunApkTests /p:ExcludeCategories=InetAccess /p:IncludeCategories=Whatever

I'm not immediately sure how useful such a thing is, but if we're going to have $(IncludeCategories) at all, then we should sensibly support both...

I think that this will work sanely:

<PropertyGroup>
  <_IncludeCats Condition=" '$(IncludeCategories)' != '' ">include=$(IncludeCategories)</_IncludeCats>
  <_ExcludeCats Condition=" '$(ExcludeCategories)' != '' ">exclude=$(ExcludeCategories)</_ExcludeCats >
</PropertyGroup>
...
<RunInstrumentationTests
    InstrumentationArguments="$(_IncludeCats);$(_ExcludeCats)"
    ...
/>

<RunInstrumentationTests
Condition=" '%(TestApk.InstrumentationType)' != ''"
AdbTarget="$(_AdbTarget)"
AdbOptions="$(AdbOptions)"
Component="%(TestApk.Package)/%(TestApk.InstrumentationType)"
NUnit2TestResultsFile="%(TestApk.ResultsPath)"
InstrumentationArguments="$(_IncludeCategories);$(_ExcludeCategories)"
Copy link
Member Author

@jonathanpeppers jonathanpeppers Nov 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is better: it lets you specify include/exclude at the same time.

My example that worked:

make run-apk-tests INCLUDECATEGORIES=InetAccess EXCLUDECATEGORIES=Bologna

Of course no category is named Bologna, but the command in the log was:

Tool /Users/jonathanpeppers/android-toolchain/sdk/platform-tools/adb execution started with arguments:   shell am instrument  -e "include" "InetAccess" -e "exclude" "Bologna" -w "Mono.Android_Tests/xamarin.android.runtimetests.TestInstrumentation"


`INCLUDECATEGORIES` and `IncludeCategories` function in the same fashion.

To specify multiple categories, delimit each category with a `,` character.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should , be used as a delimiter?

The problem is, it can't be used on the command-line:

$ msbuild /nologo Xamarin.Android.sln /t:RunApkTests /p:ExcludeCategories=InetAccess,Bologna
MSBUILD : error MSB1006: Property is not valid.
Switch: Bologna

For switch syntax, type "MSBuild /help"

This is why : was chosen as the separator char for e.g. $(AndroidSupportedTargetJitAbis), as it can be specified on the command-line.

@jonathanpeppers jonathanpeppers force-pushed the nunitlite-exclude-include branch 2 times, most recently from 1167300 to c7c57a0 Compare November 14, 2017 20:41
In some QA needs to run tests on devices without a network connection,
so an option to disable tests that rely on the network in Mono.Android-Tests
is needed.

NUnit has the option to include or exclude tests based on the
`CategoryAttribute`. Various tests were decorated with
`[Category("InetAccess")]` that appeared to use the internet. To verify
this works, I ran the tests with my internet disabled. The test run for
Mono.Android-Tests went down from 106 to 84 tests, with no failures.

To exclude a category, on Windows:
    msbuild Xamarin.Android.sln /t:RunApkTests /p:ExcludeCategories=InetAccess

On MacOS/Linux:
    make run-apk-tests EXCLUDECATEGORIES=InetAccess

If you want to specify multiple categories, use `:` (colon) to delimit
each category. This delimiter is used for various values throughout
Xamarin.Android's build because it works well from the command line for both
MSBuild and make.

I also added the option specify `Include` as well, but it isn't particularly
useful for this case.
@jonpryor jonpryor merged commit 096210c into dotnet:master Nov 14, 2017
jpobst added a commit that referenced this pull request Aug 8, 2022
jonpryor pushed a commit that referenced this pull request Aug 24, 2022
Fixes: #7234

Changes: dotnet/java-interop@a5756ca...e31d9c6

  * dotnet/java-interop@e31d9c62: Context: #7285 (comment) (#1029)
  * dotnet/java-interop@d3ea180c: [generator] Add support for `[ObsoletedOSPlatform]` (#1026)
  * dotnet/java-interop@6d1ae4ee: [generator] Remove [Obsolete] style compatibility hacks. (#1025)
  * dotnet/java-interop@440c05ee: [generator] Refactor logic for applying [Obsolete] attributes (#1024)
  * dotnet/java-interop@9b1d3ab7: [Localization] Import translated resx files (#1018)

`generator` can now emit `[ObsoletedOSPlatformAttribute]`.  Requires:

  - Update `Mono.Android.targets` to pass
    `lang-feature=obsoleted-platform-attributes` to `generator` when
    building for .NET 7+

  - Update `acceptable-breakages-vReference-net7.0.txt` to account
    for removing existing `[Obsolete]` attributes in favor of the new
    ones, for .NET 7+ only
jonathanpeppers pushed a commit that referenced this pull request Aug 24, 2022
Fixes: #7234

Changes: dotnet/java-interop@a5756ca...e31d9c6

  * dotnet/java-interop@e31d9c62: Context: #7285 (comment) (#1029)
  * dotnet/java-interop@d3ea180c: [generator] Add support for `[ObsoletedOSPlatform]` (#1026)
  * dotnet/java-interop@6d1ae4ee: [generator] Remove [Obsolete] style compatibility hacks. (#1025)
  * dotnet/java-interop@440c05ee: [generator] Refactor logic for applying [Obsolete] attributes (#1024)
  * dotnet/java-interop@9b1d3ab7: [Localization] Import translated resx files (#1018)

`generator` can now emit `[ObsoletedOSPlatformAttribute]`.  Requires:

  - Update `Mono.Android.targets` to pass
    `lang-feature=obsoleted-platform-attributes` to `generator` when
    building for .NET 7+

  - Update `acceptable-breakages-vReference-net7.0.txt` to account
    for removing existing `[Obsolete]` attributes in favor of the new
    ones, for .NET 7+ only
@jonathanpeppers jonathanpeppers deleted the nunitlite-exclude-include branch April 26, 2023 15:31
@github-actions github-actions bot locked and limited conversation to collaborators Feb 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants