AnsweredAssumed Answered

NUnit & Project.OpenAsync()

Question asked by dastewart on Sep 24, 2020
Latest reply on Oct 19, 2020 by dastewart

I'm pretty new to Pro SDK development and async coding, so this is probably a rookie mistake.

 

I'm attempting to us NUnit to do testing of our add-in. I plan to develop a public API to the add-in, so plan for NUnit tests to use that public API. I have already constructed and tested an NUnit test suite and a simple API that does no Pro SDK calls; they work as expected. So I know NUnit is set up correctly and can successfully call methods in the public API class. However, when I create the first test and API method that call a Pro SDK method, Project.OpenAsync() to open a project file, it fails with a NullReferenceException. You will see in the code below that the path being passed to Project.OpenAsync() has already been vetted as valid. ArcGIS Pro has no problem opening said path either. So the input appears good. What could I be missing?

 

The NUnit test suite:

 

using System.Threading;
using System.Threading.Tasks;
using ArcGIS.Core.Hosting;
using NUnit.Framework;
using publisher_arcgispro;

namespace NUnit.Tests
{
    [TestFixture]
    public class GeoPdfExportTestSuite
    {
        [SetUp]
        public void TestSetUp()
        {
        }

        [TearDown]
        public void TestTearDown()
        {
        }

        [Test(ExpectedResult = 0), Description("basic export"), Apartment(ApartmentState.STA)]
        public async Task<int> Test1()
        {
            string projectPath = @"C:\devel\Pro test data\subtypes.aprx";
            string layoutPath = "layout/layout.xml";


            Host.Initialize();


            int result = await API.GeoPdfExport.ExportLayout(projectPath,
                                                             layoutPath);
            return result;
        }
    }
}

The public API:

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using ArcGIS.Desktop.Core;
using ArcGIS.Desktop.Framework.Threading.Tasks;
    
namespace publisher_arcgispro
{
    public class API
    {
        public class GeoPdfExport
        {
            public static int dummy()
            {
                return 1;
            }

            public static async Task<int> ExportLayout(string projectPath,
                                                       string layoutPath)
            {
                FileInfo info = new FileInfo(projectPath);
                if (!info.Exists)
                    return 1;

                Project project = await Project.OpenAsync(projectPath);
                Item item = project.FindItem(layoutPath);

                return 0;
            }
        }
    }
}

Outcomes