mirakuru, your tests little helper

Posted: [Source]
Tags:  mirakuru python testing

Just yesterday, we released first version of mirakuru. Mirakuru is a helpful tools that lets add superpowers to Your tests, or maybe other scripts that need other processes to run.

With few lines of code, mirakuru guarantees, that when running, your script will start it's own database instance, or the webserver you've just wrote, and need to test it by sending real requests to it - automatically.

mirakuru is a fork of a summon_process library, we use a lot in both our tests, and pytest-dbfixture package, and it coded to work both in python 2.7, 3.2, 3.3 and 3.4. We also simplified package structure, api, striped some specialized context manager code and change packages's that, in our opinion better reflect it's character and dangers it brings.

Why the mirakuru name then?

If you've seen Arrow TV series, you'll already know, if You haven't, mirakuru was a substance, a super-soldier serum invented by Japanese scientists during World War II. It gave injected person a super human abilities, speed, reflex, strength, healing - (Something like Cpt. America from Marvel's Universe). And just as mirakuru there, this package, will allow your tests to run without You to care about database service being running on your test machine.

But mirakuru there had also a side effect, their user was rendered emotionally unstable, unpredictable, and hallucinating. And same here, You've got to watch on how many processes you start, and what they're doing.

It might also make writing integration tests much easier than unittest, but as long as they work, you might forget that it's easier and faster to fix some behaviour on unittest than on integration with several subprocesses. Or make you focus on completely irrelevant part of your code.

By using mirakuru, all you need to provide for CI machine are database servers executables. They'll be started along your test suite, and configured accordingly.

How does it work?

Without further babling, here's example from mirakuru's readme:

from mirakuru import HTTPExecutor
from httplib import HTTPConnection, OK


def test_it_works():
    executor = HTTPExecutor("./server", url="http://localhost:6543/")

    # start and wait for it to run
    executor.start()
    # should be running!
    conn = HTTPConnection("localhost", 6543)
    conn.request('GET', '/')
    assert conn.getresponse().status is OK
    executor.stop()

What we did here:

  1. We told executor how to start our webapp, and what http address it'll be using to receive requests.

  2. Then we start the executor.
    1. Executor first started the process

    2. Then, determined, if executor is accepting requests on hostname and port extracted from url.

    3. Once the process accepts connections, executors tries to make a successful HEAD request on url.

    4. And only after the HEAD request is successful, tests continue, and we can interact with running application underneath.

Timeout mirakuru uses, is configurable, so executor might wait 20 seconds, 2 minutes, or indefinitely. And same goes for stopping process.

Whenever Timeout is exceeded, both actions rises TimeoutException error.

What after that?

Apart from some minor enhancements, we're considering adding specialized executors for databases, or queue systems.

It could also provide a command line utility that would be able to start processes based on configuration, and tests written in other language (or one that does not use mirakuru directly).

Comments powered by Disqus