Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #100246 > unrolled thread

Re: python unit test frame work

Started byBen Finney <ben+python@benfinney.id.au>
First post2015-12-11 10:02 +1100
Last post2015-12-11 10:02 +1100
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: python unit test frame work Ben Finney <ben+python@benfinney.id.au> - 2015-12-11 10:02 +1100

#100246 — Re: python unit test frame work

FromBen Finney <ben+python@benfinney.id.au>
Date2015-12-11 10:02 +1100
SubjectRe: python unit test frame work
Message-ID<mailman.122.1449788544.12405.python-list@python.org>
Ganesh Pal <ganesh1pal@gmail.com> writes:

> I have multiple checks if I don't meet them continuing with the main
> program doesn't make sense

That means these are not unit tests (which are isolatable, independent
test cases).

If the tests are best characterised as a sequence of steps, then the
‘unittest’ model (designed for actual unit tests) will not fit well.

You should instead just write a ‘main’ function that calls each test
case in turn and exits when one of them fails.

    import sys

    from .. import foo_app


    def test_input_wibble_returns_lorem(frob):
        """ Should process 'wibble' input and result in state 'lorem'. """
        frob.process("wibble")
        if frob.state != "lorem":
            raise AssertionError


    def test_input_warble_returns_ipsum():
        """ Should process warble' input and result in state 'ipsum'. """
        frob.process("warble")
        if frob.state != "ipsum":
            raise AssertionError


    # …

    EXIT_STATUS_ERROR = 1

    def main(argv):
        """ Mainline code for this module. """

        process_args(argv)

        test_cases = [
                test_input_wibble_returns_lorem,
                test_input_warble_returns_ipsum,
                # …
                ]

        test_frob = foo_app.Frob()

        for test_case in test_cases:
            try:
                test_case(test_frob)
            except AssertionError as exc:
                sys.stderr.write("Test run failed ({exc})".format(exc=exc))
                sys.exit(EXIT_STATUS_ERROR)


    if __name__ == "__main__":
        exit_status = main(sys.argv)
        sys.exit(exit_status)

-- 
 \        “The greatest tragedy in mankind's entire history may be the |
  `\       hijacking of morality by religion.” —Arthur C. Clarke, 1991 |
_o__)                                                                  |
Ben Finney

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web