Class TestHelper
Misc local unit test helpers.
Namespace: Neon.Xunit
Assembly: Neon.Xunit.dll
Syntax
public static class TestHelper
Fields
ClusterTestingVariable
The presence of this environment variable indicates that NEONKUBE cluster based unit tests should be enabled.
Declaration
public const string ClusterTestingVariable = "NEON_CLUSTER_TESTING"
Field Value
| Type | Description |
|---|---|
| string |
IsClusterTestingEnabled
Indicates whether NEONKUBE cluster based testing is enabled by the presence
of the NEON_CLUSTER_TESTING environment variable.
Declaration
public static bool IsClusterTestingEnabled
Field Value
| Type | Description |
|---|---|
| bool |
Properties
NeonForgeTestAssetsFolder
Returns the path to the NEONFORGE project test assets folder. This folder is used to hold various assets required by some NEONFORGE unit tests.
Declaration
public static string NeonForgeTestAssetsFolder { get; }
Property Value
| Type | Description |
|---|---|
| string |
Exceptions
| Type | Condition |
|---|---|
| NotSupportedException | Thrown when the development environment is not fully configured. |
Methods
AssertEqualLines(string, string)
Compares two strings such that platform line ending differences will be ignored. This works by removing any embedded carriage returns before performing the comparision.
Declaration
public static void AssertEqualLines(string expected, string actual)
Parameters
| Type | Name | Description |
|---|---|---|
| string | expected | The expected value. |
| string | actual | The actual valut. |
Exceptions
| Type | Condition |
|---|---|
| AssertException | Thrown when the strings are not equal after removing CR characters. |
AssertEquivalent<T>(IEnumerable<T>, IEnumerable<T>)
Ensures that two enumerations contain the same items, possibly in different orders. This is similar to Xunit collection comparison assert method but it doesn't enforce the item order. This uses the default equality comparer.
Declaration
public static void AssertEquivalent<T>(IEnumerable<T> expected, IEnumerable<T> collection)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<T> | expected | The expected items. |
| IEnumerable<T> | collection | The collection being tested. |
Type Parameters
| Name | Description |
|---|---|
| T |
Exceptions
| Type | Condition |
|---|---|
| AssertException | Thrown on failure. |
AssertEquivalent<T>(IEnumerable<T>, IEnumerable<T>, IEqualityComparer<T>)
Ensures that two enumerations contain the same items, possibly in different orders. This is similar to Xunit collection comparison assert method but it doesn't enforce the item order. This uses a custom equality comparer.
Declaration
public static void AssertEquivalent<T>(IEnumerable<T> expected, IEnumerable<T> collection, IEqualityComparer<T> comparer)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<T> | expected | The expected items. |
| IEnumerable<T> | collection | The collection being tested. |
| IEqualityComparer<T> | comparer | The comparer used to equate objects in the collection with the expected object |
Type Parameters
| Name | Description |
|---|---|
| T |
Exceptions
| Type | Condition |
|---|---|
| AssertException | Thrown on failure. |
AssertEquivalent<TKey, TValue>(IDictionary<TKey, TValue>, IDictionary<TKey, TValue>)
Ensures that two dictionaries contain the same items using the default equality comparer.
Declaration
public static void AssertEquivalent<TKey, TValue>(IDictionary<TKey, TValue> expected, IDictionary<TKey, TValue> dictionary)
Parameters
| Type | Name | Description |
|---|---|---|
| IDictionary<TKey, TValue> | expected | The expected items. |
| IDictionary<TKey, TValue> | dictionary | The collection being tested. |
Type Parameters
| Name | Description |
|---|---|
| TKey | Specifies the dictionary key type. |
| TValue | Specifies the dictionary value type. |
Exceptions
| Type | Condition |
|---|---|
| AssertException | Thrown on failure. |
AssertEquivalent<TKey, TValue>(IDictionary<TKey, TValue>, IDictionary<TKey, TValue>, IEqualityComparer<TValue>)
Ensures that two dictionaries contain the same items using an equality comparer.
Declaration
public static void AssertEquivalent<TKey, TValue>(IDictionary<TKey, TValue> expected, IDictionary<TKey, TValue> dictionary, IEqualityComparer<TValue> comparer)
Parameters
| Type | Name | Description |
|---|---|---|
| IDictionary<TKey, TValue> | expected | The expected items. |
| IDictionary<TKey, TValue> | dictionary | The collection being tested. |
| IEqualityComparer<TValue> | comparer | The equality comparer to be used. |
Type Parameters
| Name | Description |
|---|---|
| TKey | Specifies the dictionary key type. |
| TValue | Specifies the dictionary value type. |
Exceptions
| Type | Condition |
|---|---|
| AssertException | Thrown on failure. |
AssertNotEqualLines(string, string)
Compares two strings such that platform line ending differences will be ignored. This works by removing any embedded carriage returns before performing the comparision.
Declaration
public static void AssertNotEqualLines(string expected, string actual)
Parameters
| Type | Name | Description |
|---|---|---|
| string | expected | The expected value. |
| string | actual | The actual valut. |
Exceptions
| Type | Condition |
|---|---|
| AssertException | Thrown when the strings are equal after removing CR characters. |
AssertNotEquivalent<T>(IEnumerable<T>, IEnumerable<T>)
Ensures that two enumerations are not equivalent, using the default equality comparer.
Declaration
public static void AssertNotEquivalent<T>(IEnumerable<T> expected, IEnumerable<T> collection)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<T> | expected | The expected items. |
| IEnumerable<T> | collection | The collection being tested. |
Type Parameters
| Name | Description |
|---|---|
| T |
Exceptions
| Type | Condition |
|---|---|
| AssertException | Thrown on failure. |
AssertNotEquivalent<T>(IEnumerable<T>, IEnumerable<T>, IEqualityComparer<T>)
Ensures that two enumerations are not equivalent, using a custom equality comparer.
Declaration
public static void AssertNotEquivalent<T>(IEnumerable<T> expected, IEnumerable<T> collection, IEqualityComparer<T> comparer)
Parameters
| Type | Name | Description |
|---|---|---|
| IEnumerable<T> | expected | The expected items. |
| IEnumerable<T> | collection | The collection being tested. |
| IEqualityComparer<T> | comparer | The comparer used to equate objects in the collection with the expected object |
Type Parameters
| Name | Description |
|---|---|
| T |
Exceptions
| Type | Condition |
|---|---|
| AssertException | Thrown on failure. |
AssertNotEquivalent<TKey, TValue>(IDictionary<TKey, TValue>, IDictionary<TKey, TValue>)
Ensures that two dictionaries do not contain the same items using the default equality comparer.
Declaration
public static void AssertNotEquivalent<TKey, TValue>(IDictionary<TKey, TValue> expected, IDictionary<TKey, TValue> dictionary)
Parameters
| Type | Name | Description |
|---|---|---|
| IDictionary<TKey, TValue> | expected | The expected items. |
| IDictionary<TKey, TValue> | dictionary | The collection being tested. |
Type Parameters
| Name | Description |
|---|---|
| TKey | Specifies the dictionary key type. |
| TValue | Specifies the dictionary value type. |
Exceptions
| Type | Condition |
|---|---|
| AssertException | Thrown on failure. |
AssertNotEquivalent<TKey, TValue>(IDictionary<TKey, TValue>, IDictionary<TKey, TValue>, IEqualityComparer<TValue>)
Ensures that two dictionaries do not contain the same items using an equality comparer.
Declaration
public static void AssertNotEquivalent<TKey, TValue>(IDictionary<TKey, TValue> expected, IDictionary<TKey, TValue> dictionary, IEqualityComparer<TValue> comparer)
Parameters
| Type | Name | Description |
|---|---|---|
| IDictionary<TKey, TValue> | expected | The expected items. |
| IDictionary<TKey, TValue> | dictionary | The collection being tested. |
| IEqualityComparer<TValue> | comparer | The equality comparer to be used. |
Type Parameters
| Name | Description |
|---|---|
| TKey | Specifies the dictionary key type. |
| TValue | Specifies the dictionary value type. |
Exceptions
| Type | Condition |
|---|---|
| AssertException | Thrown on failure. |
AssertThrowsAsync<TException>(Func<Task>)
Verifies that an asynchronous action throws a TException or an
AggregateException that contains TException.
Declaration
public static Task AssertThrowsAsync<TException>(Func<Task> action) where TException : Exception
Parameters
| Type | Name | Description |
|---|---|---|
| Func<Task> | action | The test action. |
Returns
| Type | Description |
|---|---|
| Task | The tracking Task. |
Type Parameters
| Name | Description |
|---|---|
| TException | The required exception type. |
AssertThrows<TException>(Action)
Verifies that an action throws a TException or an
AggregateException that contains TException.
Declaration
public static void AssertThrows<TException>(Action action) where TException : Exception
Parameters
| Type | Name | Description |
|---|---|---|
| Action | action | The test action. |
Type Parameters
| Name | Description |
|---|---|
| TException | The required exception type. |
Exceptions
| Type | Condition |
|---|---|
| AssertException | Thrown when no exception was thrown or the exception thrown had an unexpected type. |
GetUbuntuTestVhdxPath()
Returns the path to a Ubuntu VHDX manifest suitable for basic unit testing. This will be located in the NeonForgeTestAssetsFolder and will be downloaded from S3 if it's not already present.
Declaration
public static string GetUbuntuTestVhdxPath()
Returns
| Type | Description |
|---|---|
| string | Path to the VHDX file. |
Remarks
This is simply a copy of the base image used for constructing NEONKUBE node images and is Ubuntu 22.04 with these key changes (amongst others):
- User credentials are: sysadmin/sysadmin0000
- sudo password prompting is disabled.
- sshd is enabled.
- SNAP has been removed.
- unzip is installed to support LinuxSshProxy commands.
ResetDocker(Type, bool)
Resets Docker state by removing all containers, volumes, networks and optionally the Docker image cache. This is useful ensuring that Docker is in a known state. This also disables swarm mode.
Declaration
public static void ResetDocker(Type testClass, bool pruneImages = false)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | testClass | Specifies the current test class or pass |
| bool | pruneImages | Optionally prunes the Docker image cache. |
Remarks
This method works by comparing the testClass passed
with any previous test class passed. The method only resets the Docker
state when the test class changes. This prevents Docker from being reset
when every test in the same class runs (which will probably break tests).
note
This does not support multiple test classes performing parallel Docker operations.
RunFixture<T>(params object[])
Used to run a TestFixture outside of a unit test.
Declaration
public static void RunFixture<T>(params object[] args) where T : class
Parameters
| Type | Name | Description |
|---|---|---|
| object[] | args | Optional parameters that will be passed to the constructor after the fixture parameter. Note that the number of parameters and their types must match the constructor parameters after the fixture one. |
Type Parameters
| Name | Description |
|---|---|
| T | Specifies the test type. |
Remarks
This is often used to run a NeonService using NeonServiceFixture<TService> or a collection of NeonService instances for debugging purposes using a combination of a ComposedFixture with NeonServiceFixture<TService> sub-fixtures. But, this can also be used for any ITestFixture implementation.
You'll need to implement a test class that derives from a Xunit.IClassFixture<TFixture>
implementation and optionally implements IDisposable. You'll pass
your test type as T. Your test class must include a public
constructor that accepts a single parameter with the test fixture type and
a public method with no parameters called public void Run().
This will look something like:
public class MyTestRunner : IClassFixture<ComposedFixture>
{
private ComposedFixture composedFixture;
private NatsFixture natsFixture;
private NeonServiceFixture<QueueService> queueServiceFixture;
public MyTestRunner(ComposedFixture fixture)
{
this.composedFixture = fixture;
composedFixture.Start(
() =>
{
composedFixture.AddFixture("nats", new NatsFixture(),
natsFixture =>
{
natsFixture.StartAsComposed();
});
composedFixture.AddServiceFixture("queue-service", new NeonServiceFixture<QueueService>(), () => CreateQueueService());
});
this.natsFixture = (NatsFixture)composedFixture["nats"];
this.queueServiceFixture = (NeonServiceFixture<QueueService>)composedFixture["queue-service"];
}
public void Run()
{
// The runner will stop when this method returns. You can
// also use this as an opportunity to perform any initialization.
// For this example, we're just going to spin slowly forever.
while (true)
{
System.Threading.Thread.Sleep(10000);
}
}
}
This method performs these steps:
-
Perform a runtime check to verify that
Thas a public constructor that accepts a single parameter of typeTas well as any additional parameters. -
Perform a runtime check to ensure that
Thas apublic void Run()method. - Instantiate an instance of the test fixture specified by Xunit.IClassFixture<TFixture>.
-
Instantiate an instance of
T, passing the test fixture just created as the parameter. -
Call the
Run()method and wait for it to return. - Dispose the test fixture.
- Call Dispose(), if implemented by the test class.
- The method returns.
TempFolder(string, string)
Creates and populates a temporary test folder with a test file.
Declaration
public static TempFolder TempFolder(string filename, string data)
Parameters
| Type | Name | Description |
|---|---|---|
| string | filename | The file data. |
| string | data | The file name |
Returns
| Type | Description |
|---|---|
| TempFolder |
Remarks
note
Ensure that the TempFolder(string, string) returned is disposed so it and any files within will be deleted.