python unit test frame work
Ben Finney
ben+python at benfinney.id.au
Thu Dec 10 18:02:07 EST 2015
Ganesh Pal <ganesh1pal at 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
More information about the Python-list
mailing list