Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ben Finney Newsgroups: comp.lang.python Subject: Re: python unit test frame work Date: Fri, 11 Dec 2015 10:02:07 +1100 Lines: 68 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de 1LpcL2EykTncmicWdO5IzQcNQVJtvvGQWOxgs8mqqidg== Cancel-Lock: sha1:8TlCDEXdt/pJw96RcysCUfEKJdc= Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '"""': 0.05; 'sys': 0.05; '"__main__":': 0.07; '__name__': 0.07; 'subject:test': 0.07; 'exits': 0.09; 'fails.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.13; 'subject:python': 0.14; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'steps,': 0.16; 'tests)': 0.16; 'try:': 0.18; 'tests': 0.18; 'input': 0.18; 'fit': 0.23; 'import': 0.24; 'header:User-Agent:1': 0.26; "doesn't": 0.26; '(which': 0.26; 'header:X-Complaints-To:1': 0.26; 'sense': 0.26; 'module.': 0.27; 'sequence': 0.27; 'function': 0.28; 'skip:( 20': 0.28; 'actual': 0.28; 'raise': 0.29; 'code': 0.30; 'checks': 0.30; 'continuing': 0.32; 'run': 0.33; 'except': 0.34; 'unit': 0.35; 'should': 0.36; 'instead': 0.36; 'subject:work': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'turn': 0.37; 'received:org': 0.37; 'means': 0.39; 'test': 0.39; 'well.': 0.40; 'to:addr:python.org': 0.40; 'entire': 0.61; 'skip:\xe2 10': 0.70; '8bit%:43': 0.72; '1991': 0.84; '_o__)': 0.84; 'hijacking': 0.84; 'received:125': 0.84; '\xe2\x80\xa6': 0.84; '\xe2\x80\x9cthe': 0.93 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: jigong.madmonks.org X-Public-Key-ID: 0xAC128405 X-Public-Key-Fingerprint: 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-pubkey.asc X-Post-From: Ben Finney User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:100246 Ganesh Pal 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