Skip to content

R4ken/MiniTest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 

Repository files navigation

MiniTest

Lightweight unit test framework for C# written from scratch.

code runner

It consists of 2 components:

  • MiniTest library - containing test attributes allowing users to mark classes and methods as test containers and assertion methods.
  • MiniTestRunner executable - an application that dynamically loads assemblies containing tests, searches for test containers, executes found tests, and presents the test results in the console.

MiniTest

Test Attributes

The library provides the following attributes used to mark classes and methods as test containers and manage the test lifecycle.

  1. TestClassAttribute marks a class as a container for test methods.
  2. TestMethodAttribute marks a method as a unit test to be executed.
  3. BeforeEachAttribute defines a method to be executed before each test method.
  4. AfterEachAttribute defines a method to be executed after each test method.
  5. PriorityAttribute sets a priority (integer) for test prioritization, with lower numerical values indicating higher priority.
  6. DataRowAttribute enables parameterized testing by supplying data to test methods.
    • it accepts an array of objects (object?[]) representing test data.
    • optionally takes a string parameter that documents the test data set.
  7. DescriptionAttribute allows the inclusion of additional description to a test or a test class.

Assertions

The library also provides methods to verify test success or failure conditions.

  1. ThrowsException<TException>(Action action, string message = ""): Confirms that a specific exception type is thrown during a given operation.
  2. AreEqual<T>(T? expected, T? actual, string message = ""): Verifies equality between expected and actual values.
  3. AreNotEqual<T>(T? notExpected, T? actual, string message = ""): Ensures that the expected and actual values are distinct.
  4. IsTrue(bool condition, string message = ""): Confirms that a boolean condition is true.
  5. IsFalse(bool condition, string message = ""): Confirms that a boolean condition is false.
  6. Fail(string message = ""): Explicitly fails a test with a custom error message.

Additionally, all methods take an optional message string that describes the purpose of the assertion.
An example implementation of IsTrue:

public static void IsTrue(bool condition, string message = "")
{
    if (!condition)
    {
        throw new AssertionException(message);
    }
}

Example

AuthenticationService.Tests already includes tests marked with attributes described earlier. Can be used as an example

MiniTestRunner

The MiniTestRunner is a console application that handles the discovery and execution of tests, providing output and summaries upon completion.

Input

The application takes the assembly file paths as command-line arguments. These assemblies should contain the test classes and methods defined using the MiniTest framework.

Example:

MiniTestRunner path/to/test-assembly1.dll path/to/test-assembly2.dll

Test Execution

Methods are executed in order based on the PriorityAttribute, where a lower numerical value indicates a higher priority. Methods without the attribute have a priority of 0. Methods with the same priority should be executed in alphabetical order based on the method name.

For each test class, for each test, runner executes:

  1. BeforeEach method
  2. The test method itself
  3. AfterEach method

In parameterized tests, each data row provided via DataRowAttribute is executed separately (counts as a separate test), passing the specified parameters to the test method.

Test counts as failed if, during the execution of the test method, an unhandled exception was thrown.

Output and Formating

Output shown for each test:

  • Test status: PASSED or FAILED.
  • Failure reasons and exception messages for failed tests.
  • Output the description (if provided) for the class or test method.

Output shown for each class:

  • Total number of tests in a class.
  • Number of passed and failed tests in a class.

After running all tests from an assembly, summary is shown.

Output is colorized for better readability:

  • Green for passed tests.
  • Red for failed tests.
  • Yellow for test-related warnings (e.g., incompatible configuration, no parameterless constructor).

Additional remarks

Project was made for Programming 3 - advanced classes

About

Lightweight unit test framework for C# written from scratch

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  

Languages