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


Groups > comp.lang.python > #100246

Re: python unit test frame work

From Ben Finney <ben+python@benfinney.id.au>
Newsgroups comp.lang.python
Subject Re: python unit test frame work
Date 2015-12-11 10:02 +1100
Message-ID <mailman.122.1449788544.12405.python-list@python.org> (permalink)
References <CACT3xuUOrbdkiEDkDMcVxCO+td4setJMb2TamW5te3FQK8oL0g@mail.gmail.com> <n4c706$g88$1@ger.gmane.org> <CACT3xuUzRn5Sq8V5zqLYx-QFuKkTKJHvcJGLTT9HLSTKPBL_tw@mail.gmail.com> <CACT3xuWxoQdg4P_5GCGVLXEWcDk09sFoROuZ8=Ukox42_kMc_w@mail.gmail.com> <CACT3xuXS5zcppQ4W0dxigi=Pv99doH81_KpttR=+3WSrOuOzzw@mail.gmail.com>

Show all headers | View raw


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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web