Skip to main content
Lesson 1

Creating a Test Project and a Test Class

Back to T280: Testing Business Logic

Lesson 1: Creating a Test Project and a Test Class

Before you begin creating unit tests in the Unit Test Framework, you need to create a Visual Studio project, which you will set up to be a test project. In the test project, you create a test class for each graph or graph extension you want to test. The topics of this chapter describe how to create and configure a Visual Studio project that is intended to contain unit tests for an Acumatica Framework-based application and how to create test classes.

Test Project and Test Class: General Information

As the first step in creating a set of unit tests for an extension library in a customization project, you create a Visual Studio project and configure it to serve as the test project. For each graph or graph extension to be tested, you create a separate test class in the test project.

Learning Objectives

In this lesson, you will learn how to do the following:

  • Configure a project in Visual Studio to be a test project that uses the xUnit.net library and the Acumatica Unit Test Framework
  • Create the test class that will contain unit tests

Applicable Scenarios

You create and configure a test project each time you need to create unit tests for graphs and graph extensions implemented in a customization project. You create a test class for each graph or graph extension for which you will create unit tests.

Requirements for Using the Acumatica Unit Test Framework

By design, the Acumatica Unit Test Framework does not require you to sign in or access a database on a disk. To perform unit tests, you do not need to deploy an Acumatica ERP instance or a database instance. All actions are performed in RAM. The Acumatica Unit Test Framework is designed to be used with the xUnit.net library. You may configure your project for using another unit test library (for example, NUnit), but Acumatica does not guarantee compatibility with a unit test library other than xUnit.net. For details about xUnit.net library, see Getting Started with xUnit.net.

Naming Conventions for Test Projects, Namespaces, and Test Classes By performing the usual process, for each project whose functionality is going to be tested, you create a test project in the same Visual Studio solution and name it by adding .Tests to the end of the name of the project being tested. Similarly, the namespace used in the test project should be named the same as the namespace of the project being tested with .Tests appended to the name. The test class should also be named the same as the class being tested with Tests appended to the name. For example, to test the InventoryItemMaint class, which is in the PhoneRepairShop_Code namespace and contained in the PhoneRepairShop_Code.csproj project, you create Lesson 1: Creating a Test Project and a Test Class | 10

the PhoneRepairShop_Code.Tests.csproj project; in this project, you create the
InventoryItemMaintTests class and place it in the PhoneRepairShop_Code.Tests namespace.

Test Class Definition

A test class is derived from the TestBase class, which is available in the PX.Tests.Unit namespace.

Activity 1.1: To Create a Test Project

The following activity will walk you through the process of creating and configuring a test project.

Process Overview

In Visual Studio, you will create a test project in the same solution with the extension library that implements
the graphs and graph extensions being tested. You will add the required NuGet packages to the test project. You
will also add the PX.Tests.Unit.dll library and other libraries from the Acumatica ERP instance to the test
project.

Step 1: Creating a Test Project To create the PhoneRepairShop_Code.Tests.csproj project, proceed as follows:

  1. Open the PhoneRepairShop_Code.sln file, which is located in the \App_Data\Projects \PhoneRepairShop subfolder of the SmartFix_T280 instance folder.
  2. In Visual Studio, in the Solution Explorer panel, right-click the PhoneRepairShop_Code solution.
  3. Select Add > New Project.
  4. Select the Class Library (.NET Framework) project template.
  5. Click Next.
  6. Enter the PhoneRepairShop_Code.Tests project name, leave the value in the Location box unchanged, and specify .NET Framework 4.8 in the Framework box.
  7. Click Create.
  8. Delete the Class1.cs item from the PhoneRepairShop_Code.Tests.csproj project. You will create the required C# classes later.

Step 2: Adding NuGet Packages Do the following to add the necessary NuGet packages to the PhoneRepairShop_Code.Tests.csproj project:

  1. In the Solution Explorer panel of Visual Studio, right-click the PhoneRepairShop_Code.Tests project, and select Manage NuGet Packages.
  2. Install the following packages:
  • xunit (Version 2.4.2)
  • xunit.runner.visualstudio (Version 2.4.5)
  • System.Runtime.CompilerServices.Unsafe (Version 6.0.0)
  • Microsoft.Bcl.AsyncInterfaces (Version 8.0.0)
  • Moq (Version 4.20.72) Lesson 1: Creating a Test Project and a Test Class | 11

Step 3: Adding References to the Customization Code and Acumatica ERP Libraries To add references to the customization project and Acumatica ERP libraries to the PhoneRepairShop_Code.Tests.csproj project, proceed as follows:

  1. Copy the PX.Tests.Unit.dll file from the UnitTesting subfolder of the Acumatica ERP installation folder to any folder you choose.
  2. Right-click the References item of the PhoneRepairShop_Code.Tests project, and select Add Reference.
  3. On the Browse tab, click Browse.
  4. In the SmartFix_T280\Bin folder, select the following files:
  • PX.Common.dll
  • PX.Common.Std.dll (this library makes it possible to use graph views in the code)
  • PX.Data.BQL.Fluent.dll
  • PX.Data.dll
  • PX.Objects.dll (this library contains the implementation of the PX.Objects.IN.InventoryItem class)
  1. Select the PX.Tests.Unit.dll file in the folder where you have placed it.
  2. On the Projects tab, select the PhoneRepairShop_Code project.
  3. Click OK.

Activity 1.2: To Create a Test Class

The following activity will walk you through the process of creating a test class in the test project that you have
created.

Story

You need to create a class for testing the Repair Services (RS201000) custom form, whose business logic is
implemented in the RSSVRepairServiceMaint class.

Process Overview

In the test project, you create a test class derived from the PX.Tests.Unit.TestBase class. This class is
intended for containing test methods.

Step: Creating a Test Class To create a test class for the Repair Services (RS201000) form, do the following:

  1. In the Solution Explorer panel of Visual Studio, right-click the PhoneRepairShop_Code.Tests project, and select Add > New Item.
  2. Select the Visual C# item | Class template, and type RSSVRepairServiceMaintTests.cs as the file name.
  3. Click Add.
  4. Specify the following using directives in the RSSVRepairServiceMaintTests.cs file.
         using Xunit;
    

Lesson 1: Creating a Test Project and a Test Class | 12

    using PX.Data;
    using PX.Tests.Unit;
    using PhoneRepairShop;

5. Make the RSSVRepairServiceMaintTests class public and derived from TestBase as follows.

        public class RSSVRepairServiceMaintTests : TestBase

In this class, you will create a test method to test the logic of the RSSVRepairServiceMaint graph, as described in Activity 2.1: To Create a Test Method Without Parameters. Lesson 2: Creating a Test Method | 13