Products Technologies Demo Docs Blog Support Company

Unit Testing with NUnit and MSTest and TX Text Control .NET 32.0

Unit testing is a common practice in which code is tested in isolation. This article shows how to use TX Text Control .NET 32.0 in such environments, and in particular how to handle licensing.

Unit Testing with NUnit and MSTest and TX Text Control .NET 32.0

Important Notice

This article explains how to use build servers for the following versions and requirements:

  • TX Text Control .NET Server 32.0 SP2 (or better)
  • TX Text Control .NET Server for for Windows Forms 32.0 SP2 (or better)
  • TX Text Control .NET Server for for WPF 32.0 SP2 (or better)
  • .NET Framework 4.5, .NET 6 or better

Due to changes in the licensing mechanism, TX Text Control 32.0 SP2 now fully supports unit testing. This tutorial shows how to create a simple class library that uses TX Text Control and how to test it using NUnit.

Creating the Application

Make sure that you downloaded the latest version of Visual Studio 2022 that comes with the .NET 6 SDK.

Prerequisites

The following tutorial requires a trial version of TX Text Control .NET Server.

  1. In Visual Studio 2022, create a new project by choosing Create a new project.

  2. Select Console App as the project template and confirm with Next.

  3. Choose a name for your project and confirm with Next.

  4. In the next dialog, choose .NET 6 (Long-term support) as the Framework and confirm with Create.

Adding the NuGet Package

  1. In the Solution Explorer, select your created project and choose Manage NuGet Packages... from the Project main menu.

    Select Text Control Offline Packages from the Package source drop-down.

    Install the latest versions of the following package:

    • TXTextControl.TextControl.ASP.SDK

    ASP.NET Core Web Application

Adding Code

A sample method using the ServerTextControl class is implemented in this step.

  1. Open the code view of the Class1 file you created and replace the code with the following code.

    namespace MyClassLibrary
    {
        public static class Class1
        {
            static Class1()
            {
                TXTextControl.ServerTextControl.EntryAssembly =
                    typeof(Class1).Assembly;
            }
    
            public static string GetHtml()
            {
                using (TXTextControl.ServerTextControl tx = 
                    new TXTextControl.ServerTextControl())
                {
                    tx.Create();
    
                    TXTextControl.Selection selection = 
                        new TXTextControl.Selection();
                    selection.Text = "Hello, World!";
                    selection.Start = 0;
                    selection.Length = 5;
                    selection.Bold = true;
    
                    tx.Selection = selection;
    
                    var html = "";
                    tx.Save(out html, TXTextControl.StringStreamType.HTMLFormat);
    
                    return html;
                }
            }
        }
    }

    Note the static constructor that sets the EntryAssembly property to the class library itself, so that the calling assembly of the unit test doesn't require a license.

Using Windows Forms or WPF?

The above code uses the ServerTextControl class. The entry assembly must be defined for each Text Control type used:

  • ServerTextControl
    TXTextControl.ServerTextControl.EntryAssembly = typeof(Class1).Assembly;
  • TextControl
    TXTextControl.TextControl.EntryAssembly = typeof(Class1).Assembly;
  • WPF.TextControl
    TXTextControl.WPF.TextControl.EntryAssembly = typeof(Class1).Assembly;

Adding Test Project

Now, we are going to add a new project to the solution that contains the unit tests.

  1. In the Solution Explorer, select your created solution and choose Add -> Project... from the File main menu.

  2. In the project templates, search for Test and choose NUnit Test Project.

    ASP.NET Core Web Application

  3. Choose a name for your project and confirm with Next.

  4. In the next dialog, choose .NET 6 (Long-term support) as the Framework and confirm with Create.

Adding the Project Reference

Now, we are adding a reference to the project that contains the code to test.

  1. In the Solution Explorer, select your created test project and choose Add Project Reference... from the Project main menu.

  2. In the Projects tab, check the project that contains the code to test and confirm with OK. Your Solution Explorer will look similar to the one shown in the next screenshot.

    ASP.NET Core Web Application

Adding the Tests

  1. Open the code view of the UnitTest1 file you created and replace the code with the following code.

    namespace MyUnitTest
    {
        public class Tests
        {
            [SetUp]
            public void Setup()
            {
            }
    
            [Test]
            public void Test1()
            {
                var html = MyClassLibrary.Class1.GetHtml();
                // if html contains "Hello, World!" the test passes
                Assert.IsTrue(html.Contains("Hello, World!"));
            }
        }
    }

Running the Tests

Now, we are ready to run the tests.

  1. Choose Run All Tests from the Test main menu.

  2. The test results are shown in the Test Explorer.

    ASP.NET Core Web Application

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

ASP.NET

Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.

ASP.NET Core
Angular
Blazor
JavaScript
React
  • Angular
  • Blazor
  • React
  • JavaScript
  • ASP.NET MVC, ASP.NET Core, and WebForms

Learn more Trial token Download trial

Related Posts

ASP.NETWindows FormsWPF

TX Text Control 33.0 SP3 is Now Available: What's New in the Latest Version

TX Text Control 33.0 Service Pack 3 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…


ASP.NETWindows FormsWPF

TX Text Control 33.0 SP2 is Now Available: What's New in the Latest Version

TX Text Control 33.0 Service Pack 2 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…


ASP.NETWindows FormsWPF

Document Lifecycle Optimization: Leveraging TX Text Control's Internal Format

Maintaining the integrity and functionality of documents throughout their lifecycle is paramount. TX Text Control provides a robust ecosystem that focuses on preserving documents in their internal…


ActiveXASP.NETWindows Forms

Expert Implementation Services for Legacy System Modernization

We are happy to officially announce our partnership with Quality Bytes, a specialized integration company with extensive experience in modernizing legacy systems with TX Text Control technologies.


ActiveXASP.NETWindows Forms

Service Pack Releases: What's New in TX Text Control 33.0 SP1 and 32.0 SP5

TX Text Control 33.0 Service Pack 1 and TX Text Control 32.0 Service Pack 5 have been released, providing important updates and bug fixes across platforms. These service packs improve the…