Unit testing of asynchrononous code in Nim
Go to file
Mark Spanbroek a236a5f0f3 Version 0.3.2 2022-07-21 11:00:47 +02:00
.github/workflows Run CI also on older version of Nim 2021-07-07 10:36:37 +02:00
asynctest Fix crash when unhandled exception is cleared by teardown 2022-07-21 10:52:57 +02:00
testmodules Support Nim 1.2.x 2022-07-21 10:52:57 +02:00
.editorconfig Asynchronous testing in Nim 2021-01-11 13:35:19 +01:00
.gitignore Add setup files (#2) 2022-07-12 23:30:36 +03:00
License.md Add license 2022-01-10 11:14:39 +01:00
Readme.md Version 0.3.2 2022-07-21 11:00:47 +02:00
asynctest.nim Add support for unittest2 2021-07-07 10:33:52 +02:00
asynctest.nimble Version 0.3.2 2022-07-21 11:00:47 +02:00
config.nims Add setup files (#2) 2022-07-12 23:30:36 +03:00
nimble.lock Add setup files (#2) 2022-07-12 23:30:36 +03:00

Readme.md

asynctest

Complements the standard unittest module in Nim to allow testing of asynchronous code.

Installation

Use the Nimble package manager to add asynctest to an existing project. Add the following to its .nimble file:

requires "asynctest >= 0.3.2 & < 0.4.0"

Usage

Simply replace import unittest with import asynctest, and you can await asynchronous calls in tests, setup and teardown.

Example


import asynctest
import asyncdispatch # alternatively: import chronos

proc someAsyncProc {.async.} =
  # perform some async operations using await

suite "test async proc":

  setup:
    # invoke await in each test setup:
    await someAsyncProc()

  teardown:
    # invoke await in each test teardown:
    await someAsyncProc()

  test "async test":
    # invoke await in tests:
    await someAsyncProc()

setupAll and teardownAll

The setup and teardown code runs before and after every test, just like the standard unittest module. In addition we provide setupAll and teardownAll. The setupAll code runs once before all tests in the suite, and the teardownAll runs once after all tests in the suite. Use these only as a last resort when setting up the test environment is very costly. Be careful that the tests do not modify the environment that you set up, lest you introduce dependencies between tests.

Unittest2

The unittest2 package is supported. Make sure that you import asynctest/unittest2 instead of the normal import.