Xamarin Forms and Test Projects (Debugging Tests)

ehainesehaines Member ✭✭

I am struggling to set up a unit test project in Xamarin Forms right now, and could use some guidance. So far all my searching has turned up bugs/issues with testing that are already resolved, or examples of running tests from within the same project.

I was able to add a new MSTest project to my Xamarin Forms solution. We are primarily using UWP, but so far nearly all our code is in the shared project, which is what I am trying to test.

It seems to run the tests, but when I try to "Debug Tests" it never hits any of the break points (and they seem to flash white, which I've often seen before when the debugger fails in other types of projects). I would really like to use the "Debug Tests" feature so I can step through the code to see where it fails. One example, I'm trying to write some back-end sync services without doing the UI for them first, and it would be really helpful to test that process without having to create the button first to start the process.

One solution I found online, which didn't work for me, was configuring the .csproj file for the shared project to "pdbonly" like so:

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
    <DebugType>pdbonly</DebugType>
    <DebugSymbols>true</DebugSymbols>
  </PropertyGroup>

Here is my test class:

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MyProject.Mobile.Services;
using MyProject.Mobile.Services.Sync;

namespace UnitTestProject
{
    [TestClass]
    public class UnitTest1
    {
        private IDatabaseService _dbService;
        private ISyncService _syncService;

        public UnitTest1()
        {
            _dbService = new DatabaseService<DatabaseContext>();
            _syncService = new SyncService(_dbService);
        }
        [TestMethod]
        public void TestMethod1()
        {
            var result = _syncService.PushInspections();
            Assert.IsNotNull(result);
        }
    }
}
  • The main (shared code) project is added as a reference and a project dependency to the test project.
  • I tried using VS 2017 and VS 2019, with the same results.

Is this kind of debugging behavior supported? If so, what am I missing?

Sign In or Register to comment.