Running NUnit tests on an Android device with `dotnet test --device` fails inside the NUnit engine

Aperta
#1,966 13 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
12/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
android, csharp

Direzione di ricerca

Start with the stack traces: DirectoryFinder.ExpandOneStep and ExtensionManager.FindExtensionAssemblies for the scan, and TestEngineActivator.CreateInstance with NUnitEngineAdapter.Initialize for the load. The open question is a design choice (make the engine device-aware or bypass it on mobile), so agree on a direction with the maintainers before writing code. Done means the androidtest repro runs NUnit tests on the emulator in Debug and Release.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Follow-up to nunit/nunit3-vs-adapter#1549, found while testing the fix for it (PR nunit/nunit3-vs-adapter#1550).

.NET 10/11 can run MTP test projects on a device with dotnet test --device. The dotnet new androidtest template does this with MSTest. With NUnit instead, the run never gets to a test. Once nunit/nunit3-vs-adapter#1549 is fixed, the failures below are inside the NUnit engine, which assumes the test assembly and the engine are files in a folder it can browse. On Android neither holds.

I'm opening this to ask what direction you'd like, rather than proposing a fix: each workaround I tried only uncovered the next failure.

Versions: NUnit 5.0.0, NUnit3TestAdapter 6.3.1 / main at 7a4456a plus the nunit/nunit3-vs-adapter#1549 fix, nunit.engine 3.22.0, Microsoft.Testing.Platform 2.5.2, .NET SDK 11.0.100, net11.0-android, x86_64 emulator (API 36), run from the command line (no IDE).

Repro:

  1. dotnet new androidtest -n DroidNUnit
  2. Replace the MSTest package with NUnit 5.0.0, NUnit3TestAdapter and Microsoft.Testing.Extensions.TrxReport (the same version as the adapter's MTP).
  3. Change <EnableMSTestRunner> to <EnableNUnitRunner> and builder.AddMSTest(...) to builder.AddNUnit(...) in TestInstrumentation.cs, convert the three tests to [Test], and remove the [assembly: Parallelize].
  4. dotnet test --device <emulator>

What fails, in order

1. Console.CancelKeyPress (#1549). Fixed in PR nunit/nunit3-vs-adapter#1550.

2. Debug build: the engine's extension scan reads a folder Android doesn't allow. ExtensionManager.FindExtensionAssemblies(Assembly) walks every parent folder of the engine up to the root, looking for NUnit.Extension.*/**/tools/. The app's assemblies are under /data/user/0/<package>/files/.__override__/x86_64/, and listing /data/user/0 is denied:

System.UnauthorizedAccessException: Access to the path '/data/user/0' is denied.
   at NUnit.Engine.Internal.DirectoryFinder.ExpandOneStep(IList`1 dirList, String pattern)
   at NUnit.Engine.Internal.DirectoryFinder.GetDirectories(IDirectory startDirectory, String pattern)
   at NUnit.Engine.Services.ExtensionManager.FindExtensionAssemblies(Assembly hostAssembly)
   at NUnit.Engine.Services.ExtensionService.StartService()
   at NUnit.Engine.TestEngine.Initialize()
   at NUnit.VisualStudio.TestAdapter.NUnitEngine.NUnitEngineAdapter.GetService[T]()
   at NUnit.VisualStudio.TestAdapter.NUnit3TestExecutor.CreateTestFilterBuilder(IDumpXml dump)

TestEngine.Initialize always adds ExtensionService, so I found no way to skip it. As far as I can see, neither the version3 branch nor main of nunit-console catches this.

3. Release build: the engine can't be loaded from a path. In Release the assemblies are packed inside the APK, so Assembly.Location is empty. TestEngineActivator.CreateInstance() builds /nunit.engine.dll from it and throws. NUnitEngineAdapter.Initialize only falls back to new TestEngine() when the activator returns null:

System.IO.FileNotFoundException: Could not load file or assembly '/nunit.engine.dll'.
   at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyPath(String)
   at NUnit.Engine.TestEngineActivator.CreateInstance()
   at NUnit.VisualStudio.TestAdapter.NUnitEngine.NUnitEngineAdapter.Initialize(IAdapterSettings)

4. With 2 and 3 patched locally, the driver can't load the test assembly. I skipped unreadable folders in DirectoryFinder and caught the activator's FileNotFoundException. The Debug run then got as far as loading the test assembly, and failed there:

NUnit.Engine.NUnitEngineException: Failed to load the test assembly /data/user/0/com.companyname.DroidNUnit/files/.__override__/x86_64/DroidNUnit.dll
   at NUnit.Engine.Drivers.NUnitNetCore31Driver.Load(String assemblyPath, IDictionary`2 settings)
   at NUnit.Engine.Runners.DirectTestRunner.LoadDriver(...)
   at NUnit.Engine.Runners.MasterTestRunner.Explore(TestFilter filter)
   at NUnit.VisualStudio.TestAdapter.NUnitEngine.NUnitEngineAdapter.Explore(TestFilter filter)

I stopped there, because it looks like the driver loading the test assembly from its file path into its own load context. That is a desktop assumption, not a one-line fix.

Question

The engine was built for desktop runners, so these are probably not individual bugs so much as a design question. For on-device runs, would you rather the engine learn to cope with a device (no browsable file system, assemblies not on disk), or have the adapter skip the engine on mobile and drive the NUnit framework directly, as the old nunit.xamarin runner did? I'm happy to help with either and to test on the emulator. I can also share the repro project and my two local patches if they're useful.

Lingua principale
C#
Stelle
236
Fork
165
Merge medio
2g 11h
PR unite (30g)
25

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di nunit/nunit-console

Tutte le issue di nunit/nunit-console

Issue simili

Altre issue su C#

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.