Development tools Testing tools. NUnit Version 7.18 This documentation is provided under restrictions on use and are protected by intellectual property laws. Except as expressly permitted in your license agreement or allowed by law, you may not use, copy, reproduce, translate, broadcast, modify, license, transmit, distribute, exhibit, perform, publish or display any part, in any form, or by any means. Reverse engineering, disassembly, or decompilation of this documentation, unless required by law for interoperability, is prohibited.

The information contained herein is subject to change without notice and is not warranted to be error-free. If you find any errors, please report them to us in writing.

© 2021 Creatio. All rights reserved. Table of Contents | 3

Table of Contents

Testing tools. NUnit 4 Add tests for the custom class 4 Example implementation algorithm 4

© 2021 Creatio. All rights reserved. Testing tools. NUnit | 4

Testing tools. NUnit

Advanced

Unit-testing (module testing) is a software development process for verifying the operation capacity of the isolated program components. The tests are usually written by developers for every advanced method of the developed class. This allows to quickly reveal the recession – errors in the tested program components. One of the NET-application Unit-testing frameworks is NUnit – Unit-testing environment with an open-source code. A special adapter has been developed to integrate it with Visual Studio. Such adapter can be installed as a Visual Studio extension or as a project NuGet package with the implemented Unit-tests. Use this link to access the 3.x version framework documentation.

To create Unit-tests for methods or Creatio custom package class properties:

1. Install NUnit Visual Studio adapter. 2. Switch to the file system development mode. 3. Set up the Unit-test project.

4. Create the tests. 5. Perform testing. Add tests for the custom class

Advanced

Example. Add tests for the custom class, implemented in the [ Source code ] type UsrNUnitSourceCode schema of the Creatio application sdkNUnit custom package.

Example implementation algorithm

1. Install NUnit Visual Studio adapter You can install NUnit Visual Studio adapter either as a Visual Studio extension or as a NuGet package.

Installing NUnit adapter as a Visual Studio extension The advantage of installing NUnit adapter as a Visual Studio extension is its availability for any test project since the adapter becomes part of IDE. Another advantage is the automatic extension update. The disadvantage is the necessity to install it for every test project team member. To install NUnit adapter:

1. Download extension from Visual Studio Marketplace *.VSIX-file.

© 2021 Creatio. All rights reserved. Testing tools. NUnit | 5

2. Double-click the *.VSIX-file and run the installation. Select the needed Visual Studio versions during installation.

Note. As an alternative, you can install NUnit adapter via the Tools > Extensions and Updates menu. Select [ Online ] filter (1) and indicate “NUnit 3 Test Adapter” (2) in the search string. Select NUnit 3 Test Adapter extension in the search results and click [ Download ]. The extension installation starts automatically.

Installing NUnit adapter as a NuGet package The advantage of NUnit adapter installation as a NuGet-package is that in this case it becomes part of Visual Studio project and is available for access to all developers who use the project. The disadvantage is the necessity to install it for all Unit-test projects. To install NUnit adapter:

1. Right-click the test project (for instance, Terrasoft.Configuration.Tests.csproj ) and select the [ Manage NuGet Packages... ] command. 2. Indicate “NUnit3TestAdapter” (1) in the search string of the opened NuGet tab. Select the package in the search results (2) and install it (3).

© 2021 Creatio. All rights reserved. Testing tools. NUnit | 6

Note. You can find detailed description of NuGet-package installation into Visual Studio projects in the “Package Manager UI” article.

2. Switch to the file system development mode Creating Unit-tests for .NET classes, implemented in Creatio packages is only possible in the file system development mode. You can find more information about the Creatio configuration development in the file system, setting up Visual Studio and the server code operation case in “Development in MS Visual Studio”, “IDE settings for development” and “Developing the server code”.

The sdkNUnit custom package containing [ Source code ] type UsrNUnitSourceCode schema is used in this case. The UsrNUnitSourceCode C# class containing methods that require writing tests is implemented in this schema source code.

Attention. You can access the custom class implementation package at sdkNUnit repository at Github.

The sdkNUnit custom package has the following view after it has been uploaded to the file system:

© 2021 Creatio. All rights reserved. Testing tools. NUnit | 7

Class source code for testing:

namespace Terrasoft.Configuration { public class UsrNUnitSourceCode { // String property. public string StringToTest { get { return "String to test"; } } // The method that verifies the equality of the two strings. public bool AreStringsEqual(string str1, string str2) { return str1 == str2; } } }

3. Set up the Unit-test project

The Terrasoft.Configuration.Tests.csproj pre-configured project is used for creating Unit-tests in this case. It is delivered with the Terrasoft.Configuration.sln solution (see “Server code operation in Visual Studio”).

Add the NUnit NuGet-package in the project dependency to use NUnit framework for creating tests in the Terrasoft.Configuration.Tests.csproj project. To do this:

1. Right-click the Terrasoft.Configuration.Tests test project in Solution Explorer and select the [ Manage NuGet Packages... ] command.

2. Indicate “NUnit” (1) in the search string of the opened NuGet package manager tab, select the package in the

© 2021 Creatio. All rights reserved. Testing tools. NUnit | 8

search results (2) and install it (3).

4. Create the tests

Note. It is common practice that the test-containing class name must have the tested class name with “Tests” word in it. It is also convenient to place tests in catalogs to group them in a project. The catalog name should match the tested package name and have “.Tests” ending in it.

To create tests for UsrNUnitSourceCode class:

1. Create sdkNUnit.Tests catalog in the Terrasoft.Configuration.Tests.csproj project.

2. Create the new UsrNUnitSourceCodeTests .class in the sdkNUnit.Tests catalog. This class source code will be stored in the UsrNUnitSourceCodeTests.cs file.

© 2021 Creatio. All rights reserved. Testing tools. NUnit | 9

3. Add the implementation test methods to the UsrNUnitSourceCodeTests class:

using NUnit.Framework;

namespace Terrasoft.Configuration.Tests.sdkNUnitTests { [TestFixture] class UsrNUnitSourceCodeTests { // The tested class instance. UsrNUnitSourceCode objToTest = new UsrNUnitSourceCode(); // Testing string. string str = "String to test";

[Test] public void ClassReturnsCorrectStringProperty() { // Testing the string property value. // The value must be populated and match the required value. string res = objToTest.StringToTest; Assert.That(res, Is.Not.Null.And.EqualTo(str)); }

[Test] public void StringsMustBeEqual() { // Testing the value equality of the two strings. bool res = objToTest.AreStringsEqual(str, "String to test"); Assert.That(res, Is.True); }

[Test] public void StringsMustBeNotEqual() { // Testing the value inequality of the two strings. // This test will fail since the values are equal. bool res = objToTest.AreStringsEqual(str, "String to test"); Assert.That(res, Is.False); } } }

The UsrNUnitSourceCodeTests class is decorated by the [ TestFixture ] attribute, which marks it as a test- containing class. Every method testing a specific functionality must be decorated by the [ Test ] attribute. You can find the description of the NUnit framework attributes in the “Attributes” NUnit article.

The testing is performed via the Assert.That() method that accepts the tested value and such value limiting

© 2021 Creatio. All rights reserved. Testing tools. NUnit | 10

objects as arguments. You can find more information about the assertions, Assert.That() method and limiting model in the “Assertions” and “Constraint Model” NUnit articles.

5. Testing To perform testing, execute the [ Test ] > [ Windows ] > [ Test Explorer ] menu command to open the [ Test Explorer ] window in Visual Studio.

Execute the [ Run All ] command to run the tests. The successfully passed tests will be moved to the [ Passed Test ] group, the failed tests will be moved to the [ Failed Test ] group.

You can find more information about the [ Test Explorer ] window functionality in the “Run unit tests with Test Explorer” Visual Studio article.

© 2021 Creatio. All rights reserved.