Giter VIP home page Giter VIP logo

surrogate's Introduction

About

surrogate is a micro-lib helping people to create stubs for non-existing modules in sys.modules so that later those modules can be imported. surrogate does not touch modules that exist in sys.modules even if you ask it to.

At the moment surrogate offers only decorator interface but it is planned to add context-manager interface as well.

Intention

Once author needed to write tests for a function that works only in production (but not in developement env). Those function imported modules that did not exist in development environment. Thus, in order to test those function, mocking of the aforementioned modules was necessary. Unfortunately, author did not manage to mock those modules with patch decorator from mock library. It was necessary to create module stubs first and then to mock them. This micro-lib does exactly what author needed (except of the mistakes, of course).

Usage

Please, use surrogate as a function decorator:

from surrogate import surrogate

@surrogate('sys.my.cool.module.stub1')
@surrogate('sys.my.cool.module.stub2')
def test_something():
    from sys.my.cool.module import stub1
    from sys.my.cool.module import stub2
    import sys.my.cool as cool
    import sys # this is a normal sys module
    do_something()

Accourding to intention, you can use surrogate with mock.patch decorators:

from surrogate import surrogate
from mock import patch

@surrogate('this.module.doesnt.exist')
@patch('this.module.doesnt.exits', whatever)
def test_something():
    from this.module.doesnt import exist
    do_something()

LICENSE

This code can be used, distributed and modified in any ways one wants. If one gets any use of it author is already rewarded. On the other hand, do not expect any guaranteed support from author. Use it as is.

surrogate's People

Contributors

ikostia avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

surrogate's Issues

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.