Skip to content
Kem Tekinay edited this page Feb 23, 2023 · 8 revisions

Setting Up

To use XojoUnit, copy the XojoUnit folder from one of the example projects to your project.

Adding your own tests is a simple process:

  1. Create a test class (e.g. MyTests) as a subclass of TestGroup.
  2. Create a subclass of TestController (e.g. XojoUnitController).
  3. Add your test subclass (MyTests) to the InitializeTestGroups event handler on XojoUnitController:
 group = New MyTests(Self, "My Tests")
  4. Create methods in your test subclass class (MyTests) to do the tests. The methods must end in “Test” in order for them to appear in the XojoUnit results.
  5. Provide a way to display the results and run the tests. For desktop apps, you want to show the TestWindow, for web apps you want to show the TestPage and for console apps you want to run the tests manually.

Testing

Standard Testing

A TestGroup has an Assert property that you will use test your assumptions. (See Assertions.) For example, Assert.AreEqual("xyz", TheTestedMethod).

There are convenience methods for gathering stats like StartTestTimer, LogTestTimer and GetTestTimer that will let you record speed measurements for various parts of your code.

Testing Asynchronous Functions

When you need to test some asynchronous method, e.g., a call to a web page, you can tell your test to "pause" using AsyncAwait(maxSeconds). Intercept the conclusion of the method and call AsyncComplete to let the framework know that the test finished, then test the results as you normally would using Assert.

If the method does not return within the specified number of seconds, the test is considered a failure.

Note that the method that handles the method's return is outside the framework so any raised Exceptions must be handled or your test app will crash. Remember to clean up any calls to AddHandler with RemoveHandler.

Testing Private Methods, Functions, and Properties

There are a few techniques to test private methods, functions, and properties.

Modules

Within a module, create a class that exposes the methods and properties, then use that to test.

Classes

  1. Create an Interface that links to private methods.
  2. Create a UnitTest subclass of your class with Test methods that expose private methods and properties.
  3. Use the ObjectSpy class.

ObjectSpy

Introduced in XojoUnit 6.8, ObjectSpy is a class that will let you access any methods or properties of another class, even private ones.

To use it, create an instance of the class you want to test, then create an instance of ObjectSpy with that test object. You can then call the methods and properties of your class through the ObjectSpy instance directly.

Example:

Var testObject As New MyClass
Var spy As New ObjectSpy(testObject)

Assert.AreEqual("result", spy.MyPrivateMethod(param1, param2).StringValue)

ObjectSpy uses Xojo's Operator_Lookup feature to match the given method or property name so there are limitations:

  1. The given method or property name must match or you will get an InvalidArgumentException at runtime.
  2. All parameters to a method must be given, even optional ones.
  3. All parameter types must match.
  4. If your method uses ParamArray, that parameter must be given as an array of the right type.
  5. A method or property with the "Hidden" attribute will not be accessible.
  6. Auto-complete will not show your test class's methods or properties.