Unit testing for traditional Synergy
With SDI you can create and run MSTest unit tests for traditional Synergy OLB and ELB projects. SDI includes a unit test project template, an item template to create a basic unit test file, unit test attributes, a TestProperties class, and an Assert class (along with AssertFailedException and AssertInconclusiveException classes) that are specific to traditional Synergy.
For information specific to traditional Synergy unit tests, see the following sections in this topic:
- Creating and running unit tests for traditional Synergy
- Attributes for traditional Synergy unit testing
- Synergex.TestFramework.TestProperties
For more information on the Assert class, see Synergex.TestFramework.Assert.
The following Microsoft unit testing features are supported: Test Explorer, VSTest.Console.exe, and .runsettings files. For information on these features and for basic information on creating and running unit tests, see Microsoft documentation (e.g., learn.microsoft.com/en-us/visualstudio/test/unit-test-basics). Third-party unit test frameworks are not supported.
Creating and running unit tests for traditional Synergy
To create and run unit tests for traditional Synergy,
1. | Create one or more unit test projects using the “Unit Test (Traditional)” template in Visual Studio, or the synNETUnitTest template in the Synergex.Projects.Templates NuGet package. Generally, you’ll create a unit test project for each traditional Synergy ELB or OLB project in the solution. See Microsoft unit test documentation (e.g., learn.microsoft.com/en-us/visualstudio/test/create-a-unit-test-project) for guidelines on creating unit tests. |
2. | In each unit test project, do the following: |
- Add a reference to the ELB or OLB project it will test.
- Add a unit test file (.dbl) for each code file or collection of related routines in the referenced project. When you create a unit test project from the “Unit Test (Traditional)” or synNETUnitTest template, the project is created with a UnitTest1.dbl file, which you can use for one of the code files in the referenced project. Use the “Basic Traditional Unit Test” item template in Visual Studio, or the synTradUnitTest template from the Synergex.ProjectItem.Templates NuGet package to create additional unit test code files.
Note that each traditional Synergy unit test project must reference SYNTRADUT:TestFramework.elb, and each unit test file must import Synergex.TestFramework. Projects created from the “Unit Test (Traditional)” or synNETUnitTest template are created with this reference, and files created from the “Basic Traditional Unit Test” item template include the import.
3. | Code the tests using the attributes listed in Attributes for traditional Synergy unit testing below and Assert methods documented in Synergex.TestFramework.Assert. |
The following example uses the {TestClass} attribute to mark UnitTest1 as a unit test class, and it uses {TestMethod} to mark the SimpleDivision method as a unit test method. In the test method, the quotient calculated by the traditional Synergy subroutine MyDivSubroutine is tested against an expected value of 2. If MyDivSubroutine returns 2 in the quotient argument, the test will pass. If it returns a different value, the AreEqual method throws an exception.
import Synergex.TestFramework namespace UnitTest1 {TestClass} public class UnitTest1 {Owner("Fred")} {TestMethod} public method SimpleDivision, void endparams proc begin data dividend ,i4, 10 data divisor ,i4, 5 data quotient ,i4, 0 data expected ,i4, 2 data delta ,i2, 0 xcall MyDivSubroutine(dividend, divisor, quotient) Assert.AreEqual(expected, quotient, delta) end endmethod endclass endnamespace
4. | Build and run the traditional Synergy unit tests. When you build a traditional Synergy unit test project, it results in an ELB that is recognized as an MSTest unit test library. You can build unit tests with Visual Studio build and rebuild commands and from the “Run All Tests” command in Visual Studio’s Test Explorer, which builds and runs all tests it can discover. You can run unit tests from Test Explorer or with VSTest.Console.exe. See Microsoft documentation (e.g., learn.microsoft.com/en-us/visualstudio/test/run-unit-tests-with-test-explorer and learn.microsoft.com/en-us/visualstudio/test/vstest-console-options) for more information, and note the following when using VSTest.Console.exe: |
- Use a Visual Studio command prompt to run VSTest.Console.exe.
- Rather than specifying .exe and .dll files, the command-line option for test file names must specify .dbr and .elb files. For example, the following command runs tests in the myTestProject.elb file:
vstest.console.exe myTestProject.elb
- If you run VSTest.Console.exe from a location other than the location of the .elb or .olb, you must specify the path for the .elb or .olb.
Attributes for traditional Synergy unit testing
The following attributes are supported for traditional Synergy unit testing. They are similar to the MSTest attributes used for .NET unit tests (see learn.microsoft.com/en-us/visualstudio/test/using-microsoft-visualstudio-testtools-unittesting-members-in-unit-tests), but they must be enclosed in curly brackets {}. By decorating classes and methods with these attributes, you specify how classes and methods are to be used for unit testing. For example, if you decorate a class with {TestClass}, the methods in that class can be used as unit test methods. See step 3 above and the information on the TestProperty attribute below for examples.
Attribute | Description |
---|---|
AssemblyCleanup | Identifies a method or routine that will be called immediately after all tests in a test library are run. Only one AssemblyCleanup attribute is supported per test library. |
AssemblyInitialize | Identifies a method or routine that will be called immediately before the tests in a test library are run. Only one AssemblyInitialize attribute is supported per test library. |
ClassCleanup | Identifies a method that will be called immediately after all tests within a class are run. Only one ClassCleanup is supported per class. |
ClassInitialize | Identifies a method that will be called immediately before the tests within a class are run. Only one ClassInitialize is supported per class. |
Ignore[(“message”)] | Causes a {TestMethod} to be skipped during test runs. If you include a message—e.g., {Ignore(“This test ignored.”)}—the message will appear in test results. |
Owner(“owner_name”) | Specifies the test owner. For example: {Owner(“Fred”)}. You can use Owner to sort and filter in Test Explorer. |
TestCategory | Assigns a unit test method to a category. Categories are optional and user-defined. You can use categories to sort and filter in Test Explorer, and from the command line you can run tests for a given category. |
TestClass | Identifies a class as a test class—i.e., a class that can contain unit test methods and properties. |
TestCleanup |
Identifies a method in a unit test class that will be called immediately after each unit test in the class is run. Test cleanup methods are used to clean up results (side effects) of tests. Only one TestCleanup attribute is supported per class. |
TestInitialize |
Identifies a method in a unit test class that will be called immediately before each test in the test class is run. Only one TestInitialize attribute is supported per class. |
TestMethod | Identifies a method (in a unit test class) as a unit test method. |
TestProperty(“property_name”, “value”) |
Defines data that may be used during runtime. This is useful for defining test information that can be used in initialization and cleanup methods to avoid unnecessary code within test methods. For example, the following uses TestProperty to define fileName as “..\testfile.txt”. {TestClass} class Test private fileName, string {TestInitialize} public method Init, void proc fileName = Synergex.TestFramework.TestProperties.GetProperty("fileName") endmethod {TestProperty("fileName", "..\testfile.txt")} {TestMethod} public method Fred proc Assert.AreEqual(this.fileName, (string)"..\testfile.txt") endmethod endclass |
Synergex.TestFramework.TestProperties
namespace Synergex.TestFramework public static class TestProperties
The Synergex.TestFramework.TestProperties class enables you to get and test properties (key value pairs) used for unit tests. This is useful for defining test information that can be used during initialization and cleanup methods to avoid unnecessary code within test methods. See the information on the TestProperty attribute in Attributes for traditional Synergy unit testing above for more information and an example.
Methods
GetProperty
public static method GetProperty(key), String
Returns the value for a property.
A property name. (System.String)
TryGetProperty
public static method TryGetProperty(key), boolean
Returns a Boolean value indicating whether or not the property exists. Returns 1 if it exists.
A property name. (System.String)