Important Notice

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

  • TX Text Control .NET Server for ASP.NET 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 for ASP.NET.

  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;
    }
    }
    }
    }
    view raw test.cs hosted with ❤ by GitHub

    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;
    view raw test.cs hosted with ❤ by GitHub
  • TextControl
    TXTextControl.TextControl.EntryAssembly = typeof(Class1).Assembly;
    view raw test.cs hosted with ❤ by GitHub
  • WPF.TextControl
    TXTextControl.WPF.TextControl.EntryAssembly = typeof(Class1).Assembly;
    view raw test.cs hosted with ❤ by GitHub

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!"));
    }
    }
    }
    view raw test.cs hosted with ❤ by GitHub

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