Git Product home page Git Product logo

funky's Introduction

get funky

Is hidden state getting you down? Don't gett down, get funky.

what?

This is a proof-of-concept Python hack that exposes the global dependencies of a function as optional keyword arguments, allowing you to easily vary them in tests.

why?

Say you've got code that looks like this:

def f(x):
  if global_config['HAHA']:
     sys.stdout.write('laugh\n')
  else:
     sys.stderr.write('cry\n')

It's wonderful, and probably correct. But how do you test it?

Essentially, you have to patch out all the references to external objects and replace them with fakes for testing. If you're using mock, you might write something like this:

@mock.patch('sys')
def testF(self, mock_sys):
    with mock.patch.dict('mymodule.global_config', {'HAHA': False}:
        f('whatever')
    mock_sys.stderr.write.assert_called_with('cry\n')

And that works for a lot of people.

What you're doing is varying os, sys, and the global_config dict and then seeing what f does in response. Which is great: that's what tests are supposed to do.

But, you can make this explicit by making those things parameters of f. That's where funky comes in.

funky_f = funkify(f)

def testF(self):
  mock_sys = mock.MagicMock()
  f('whatever', _override_sys=mock_sys,
    _override_global_config={'HAHA': False})
  mock_sys.stderr.write.assert_called_with('cry\n')

Why is this better?

It hides the details of patching and state mutation from your test, allowing your test to focus on what happens when you vary the inputs: which are all expressed as parameters.

Not only does this remove the big stack of decorators, but it makes it easier to see what exactly your tests are covering.

funky's People

Contributors

jml avatar

Watchers

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