Giter VIP home page Giter VIP logo

pytest-twisted's Introduction

pytest-twisted - test twisted code with pytest

Authors: Ralf Schmitt <[email protected]>
Version: 1.5
Date: 2014-02-04
Download:https://pypi.python.org/pypi/pytest-twisted#downloads
Code:https://github.com/schmir/pytest-twisted

pytest-twisted is a plugin for pytest, which allows to test code, which uses the twisted framework. test functions can return Deferred objects and pytest will wait for their completion with this plugin.

Installation

Install the plugin with:

pip install pytest-twisted

Using the plugin

The plugin must be enabled. This can be done in the following ways:

  1. Run py.test with the --twisted command line option

  2. Put the following into conftest.py:

    pytest_plugins = "pytest_twisted"
    
  3. Put the following into pytest.ini:

    [pytest]
    twisted = 1
    

inlineCallbacks

Using twisted.internet.defer.inlineCallbacks as a decorator for test functions, which take funcargs, does not work. Please use pytest.inlineCallbacks instead:

@pytest.inlineCallbacks
def test_some_stuff(tmpdir):
    res = yield threads.deferToThread(os.listdir, tmpdir.strpath)
    assert res == []

Waiting for deferreds in fixtures

pytest.blockon allows fixtures to wait for deferreds:

@pytest.fixture
def val():
    d = defer.Deferred()
    reactor.callLater(1.0, d.callback, 10)
    return pytest.blockon(d)

The twisted greenlet

Some libraries (e.g. corotwine) need to know the greenlet, which is running the twisted reactor. It's available from the twisted_greenlet funcarg. The following code can be used to make corotwine work with pytest-twisted:

@pytest.fixture(scope="session", autouse=True)
def set_MAIN(request, twisted_greenlet):
    from corotwine import protocol
    protocol.MAIN = twisted_greenlet

That's all.

pytest-twisted's People

Contributors

schmir avatar christianmlong avatar wjt avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.