Giter Site home page Giter Site logo

dingus's Introduction

DINGUSES

A dingus is sort of like a mock object. The main difference is that you don't set up expectations ahead of time. You just run your code, using a dingus in place of another object or class, and it will record what happens to it. Then, once your code has been exercised, you can make assertions about what it did to the dingus.

A new dingus is created from the Dingus class. You can give dinguses names, which helps with debugging your tests, especially when there are multiple dinguses in play.

>>> from dingus import Dingus >>> d = Dingus('root') >>> d <Dingus root>

Accessing any attribute of a dingus will return a new dingus.

>>> d.something <Dingus root.something>

There are a few exceptions for special dingus methods. We'll see some in a bit.

A dingus can also be called like a function or method. It doesn't care how many arguments you give it or what those arguments are. Calls to a dingus will always return the same object, regardless of the arguments.

>>> d() <Dingus root()> >>> d('argument') <Dingus root()> >>> d(55) <Dingus root()>

RECORDING AND ASSERTIONS

At any time we can get a list of calls that have been made to a dingus. Each entry in the call list contains:

  • the name of the method called (or "()" if the dingus itself was called)
  • The arguments, or () if none
  • The keyword arguments, or {} if none
  • The value that was returned to the caller

Here is a list of the calls we've made to d so far:

>>> from pprint import pprint >>> pprint(d.calls) [('()', (), {}, <Dingus root()>), ('()', ('argument',), {}, <Dingus root()>), ('()', (55,), {}, <Dingus root()>)]

You can filter calls by name, arguments, and keyword arguments:

>>> pprint(d.calls('()', 55)) [('()', (55,), {}, <Dingus root()>)]

If you don't care about a particular argument's value, you can use the value DontCare when filtering:

>>> from dingus import DontCare >>> pprint(d.calls('()', DontCare)) [('()', ('argument',), {}, <Dingus root()>), ('()', (55,), {}, <Dingus root()>)]

Dinguses can do more than just have attributes accessed and be called. They support many Python operators. The goal is to allow, and record, any interaction:

>>> d = Dingus('root') >>> (2 ** d.something)['hello']() / 100 * 'foo' <Dingus root.something.__rpow__[hello]().__truediv__.__mul__>

(Hopefully your real-world dingus recordings won't look like this!)

PATCHING

Dingus provides a context manager for patching objects during tests. For example:

>>> from dingus import patch >>> import urllib as urllib >>> with patch('urllib.request.urlopen'): ... print(urllib.request.urlopen.__class__) <class 'dingus.Dingus'> >>> print(urllib.request.urlopen.__class__) <class 'function'>

You can also use this as a decorator on your test methods:

>>> @patch('urllib.request.urlopen') ... def test_something(self): ... pass ...

ISOLATION

The opposite of patch is isolate. It patches everything except the named object:

>>> from dingus import isolate >>> @isolate('urllib.urlparse') ... def test_urlparse(self): ... pass ...

When this test runs, everything in the urllib module except urlparse will be a dingus. Note that this may be slow to execute if the module contains many objects; performance patches are welcome. :)

DANGEROUS MAGIC

Dingus can also automatically replace a module's globals when running tests. This allows you to write fully isolated unit tests. See examples/urllib/test_urllib.py for an example. The author no longer recommends this feature, as it can encourage very brittle tests. You should feel the pain of manually mocking dependencies; the pain will tell you when a class collaborates with too many others.

dingus's People

Contributors

abyx avatar anttih avatar chromy avatar ciemaar avatar dowski avatar dstanek avatar gaconnet avatar garybernhardt avatar jnrowe avatar poiati avatar

Watchers

 avatar  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.