Coder Social home page Coder Social logo

Writing unittests about alfred-workflow HOT 1 CLOSED

deanishe avatar deanishe commented on May 20, 2024
Writing unittests

from alfred-workflow.

Comments (1)

deanishe avatar deanishe commented on May 20, 2024

Typically, you shouldn't call your main function in unit tests. You should keep it simple and move the functionality to other functions/classes that you call from a barebones main. The general rule is that if you can't isolate a function to unit test it, it isn't designed well and you need to refactor the code anyway.

That won't help you capture the output of Workflow however. To do that you would temporarily reassign sys.stdout (which is what Workflow writes its XML output to) to a StringIO object and read the results from there:

from StringIO import StringIO

fp = StringIO()

# reassign sys.stdout to StringIO instance
orig_stdout = sys.stdout
sys.stdout = fp

# whatever code that makes `Workflow` do its thing here
wf.send_feedback()

# set sys.stdout back to its proper value (a `finally` clause may be a better place for this)
sys.stdout = orig_stdout

# retrieve Workflow output from the StringIO instance
self.assertEqual(fp.getvalue(), expected_value)

from alfred-workflow.

Related Issues (20)

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.