Skip to content

Commit 096210c

Browse files
jonathanpeppersjonpryor
authored andcommitted
[tests] Allow include/exclude test categories for .apks (#1024)
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]` custom attribute. Various tests that appear to use the internet are now decorated with `[Category("InetAccess")]`. 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, specify `$(ExcludeCategories)` msbuild Xamarin.Android.sln /t:RunApkTests /p:ExcludeCategories=InetAccess On MacOS/Linux, set the `EXCLUDECATEGORIES` make variable: make run-apk-tests EXCLUDECATEGORIES=InetAccess If you want to specify multiple categories, use `:` (colon) between 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. Categories can also be explicitly included, by setting the `$(IncludeCategories)` MSBuild property or the `INCLUDECATEGORIES` make variable.
1 parent f56ca4a commit 096210c

File tree

10 files changed

+46
-11
lines changed

10 files changed

+46
-11
lines changed

Documentation/DevelopmentTips.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,20 @@ For example:
178178
$ tools/scripts/xabuild /t:DeployTestApks tests/locales/Xamarin.Android.Locale-Tests/Xamarin.Android.Locale-Tests.csproj
179179
$ tools/scripts/xabuild /t:RunTestApks tests/locales/Xamarin.Android.Locale-Tests/Xamarin.Android.Locale-Tests.csproj
180180

181+
## Running `.apk` Projects with Include/Exclude
182+
183+
For example, to exclude tests that use the internet (`InetAccess` category):
184+
185+
$ make run-apk-tests EXCLUDECATEGORIES=InetAccess
186+
187+
On Windows:
188+
189+
$ msbuild Xamarin.Android.sln /t:RunApkTests /p:ExcludeCategories=InetAccess
190+
191+
`INCLUDECATEGORIES` and `IncludeCategories` function in the same fashion.
192+
193+
To specify multiple categories, delimit each category with a `:` character. The `:` delimiter works well with both `MSBuild` properties and `make` variables.
194+
181195
### Running A Single Test Fixture
182196

183197
A single NUnit *Test Fixture* -- a class with the `[TestFixture]`

build-tools/scripts/TestApks.targets

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,17 @@
152152

153153
<Target Name="RunTestApks"
154154
Condition=" '@(TestApk)' != '' ">
155+
<PropertyGroup>
156+
<_IncludeCategories Condition=" '$(IncludeCategories)' != '' ">include=$(IncludeCategories)</_IncludeCategories>
157+
<_ExcludeCategories Condition=" '$(ExcludeCategories)' != '' ">exclude=$(ExcludeCategories)</_ExcludeCategories>
158+
</PropertyGroup>
155159
<RunInstrumentationTests
156160
Condition=" '%(TestApk.InstrumentationType)' != ''"
157161
AdbTarget="$(_AdbTarget)"
158162
AdbOptions="$(AdbOptions)"
159163
Component="%(TestApk.Package)/%(TestApk.InstrumentationType)"
160164
NUnit2TestResultsFile="%(TestApk.ResultsPath)"
165+
InstrumentationArguments="$(_IncludeCategories);$(_ExcludeCategories)"
161166
TestFixture="$(TestFixture)"
162167
ToolExe="$(AdbToolExe)"
163168
ToolPath="$(AdbToolPath)">

src/Mono.Android/Test/System.Net/ProxyTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace System.NetTests {
88

9-
[TestFixture]
9+
[TestFixture, Category ("InetAccess")]
1010
public class ProxyTest {
1111

1212
// https://bugzilla.xamarin.com/show_bug.cgi?id=14968

src/Mono.Android/Test/System.Net/SslTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace System.NetTests {
1212

13-
[TestFixture]
13+
[TestFixture, Category ("InetAccess")]
1414
public class SslTest {
1515

1616
// https://xamarin.desk.com/agent/case/35534

src/Mono.Android/Test/Xamarin.Android.Net/AndroidClientHandlerTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@
3939
using Android.OS;
4040

4141
namespace Xamarin.Android.NetTests {
42-
42+
[Category("InetAccess")]
4343
public abstract class HttpClientHandlerTestBase
4444
{
45-
protected abstract HttpClientHandler CreateHandler ();
45+
protected abstract HttpClientHandler CreateHandler ();
4646

4747
class Proxy : IWebProxy
4848
{

src/Mono.Android/Test/Xamarin.Android.Net/HttpClientIntegrationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
using System.IO;
3939

4040
namespace Xamarin.Android.NetTests {
41-
41+
[Category ("InetAccess")]
4242
public abstract class HttpClientIntegrationTestBase
4343
{
4444
protected abstract HttpClientHandler CreateHandler ();

src/Xamarin.Android.NUnitLite/Gui/Activities/TestSuiteActivity.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,12 @@ protected override void OnResume ()
103103

104104
protected virtual IEnumerable <string> GetIncludedCategories ()
105105
{
106-
return null;
106+
yield break;
107107
}
108108

109109
protected virtual IEnumerable <string> GetExcludedCategories ()
110110
{
111-
return null;
111+
yield break;
112112
}
113113

114114
// Subclasses can override this method to update the test filtering that the runner will use.

src/Xamarin.Android.NUnitLite/Gui/AndroidRunner.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,8 @@ static void ChainCategoryFilter (IEnumerable <string> categories, bool negate, r
389389
gotCategories = true;
390390
}
391391

392-
chain = new AndFilter (chain, negate ? (TestFilter)new NotFilter (filter) : (TestFilter)filter);
392+
if (gotCategories)
393+
chain = new AndFilter (chain, negate ? (TestFilter)new NotFilter (filter) : (TestFilter)filter);
393394
}
394395

395396
if (!gotCategories)

src/Xamarin.Android.NUnitLite/Gui/Instrumentations/TestSuiteInstrumentation.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,22 @@ protected void AddTest (Assembly assembly)
146146

147147
protected virtual IEnumerable <string> GetIncludedCategories ()
148148
{
149-
return null;
149+
string include = arguments?.GetString ("include");
150+
if (!string.IsNullOrEmpty (include)) {
151+
foreach (var category in include.Split (':')) {
152+
yield return category;
153+
}
154+
}
150155
}
151156

152157
protected virtual IEnumerable <string> GetExcludedCategories ()
153158
{
154-
return null;
159+
string exclude = arguments?.GetString ("exclude");
160+
if (!string.IsNullOrEmpty (exclude)) {
161+
foreach (var category in exclude.Split (':')) {
162+
yield return category;
163+
}
164+
}
155165
}
156166

157167
protected virtual void UpdateFilter ()

tests/Xamarin.Android.Bcl-Tests/TestInstrumentation.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ protected override void AddTests ()
2929

3030
protected override IEnumerable<string> GetExcludedCategories ()
3131
{
32-
return App.GetExcludedCategories ();
32+
foreach (var category in base.GetExcludedCategories ()) {
33+
yield return category;
34+
}
35+
foreach (var category in App.GetExcludedCategories ()) {
36+
yield return category;
37+
}
3338
}
3439

3540
protected override void UpdateFilter ()

0 commit comments

Comments
 (0)