Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: python unit test frame work Date: Sat, 12 Dec 2015 14:49:43 +0100 Organization: None Lines: 98 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de SIrbxwlSE/2hRWsNjgSUCwO+fbe5k+7clTjR4MlXS5eQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'error:': 0.05; 'method.': 0.05; '"__main__":': 0.07; '__name__': 0.07; 'method,': 0.07; 'pretend': 0.07; 'subject:test': 0.07; 'unittest': 0.07; 'err:': 0.09; 'meaningful': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'testcases': 0.09; 'todo:': 0.09; 'example:': 0.10; 'skip:= 70': 0.10; 'python': 0.10; 'exception': 0.13; 'def': 0.13; 'subject:python': 0.14; 'instead.': 0.15; 'thu,': 0.15; '"real"': 0.16; '9:20': 0.16; '@classmethod': 0.16; 'failed")': 0.16; 'hint': 0.16; 'oddity': 0.16; 'placeholder': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'ressources': 0.16; 'run.': 0.16; 'skipped': 0.16; 'unnecessary.': 0.16; 'variable.': 0.16; 'xxx,': 0.16; 'wrote:': 0.16; 'try:': 0.18; '(in': 0.18; 'tests': 0.18; '2015': 0.20; 'class,': 0.22; 'exceptions': 0.22; 'keyerror:': 0.22; 'pass': 0.22; 'dec': 0.23; 'import': 0.24; '(most': 0.24; 'written': 0.24; 'header:User-Agent:1': 0.26; 'header:X -Complaints-To:1': 0.26; 'question': 0.27; 'actual': 0.28; 'looks': 0.29; 'cat': 0.29; 'invoke': 0.29; 'environment': 0.29; 'raise': 0.29; 'code': 0.30; 'skip:s 30': 0.31; 'post': 0.31; 'reported': 0.32; 'run': 0.33; 'class': 0.33; 'flags': 0.33; 'traceback': 0.33; 'file': 0.34; 'except': 0.34; 'handle': 0.34; 'fail': 0.35; 'expected': 0.35; 'should': 0.36; 'needed': 0.36; 'there': 0.36; 'cases': 0.36; 'subject:work': 0.36; 'to:addr :python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'setting': 0.37; 'thanks': 0.37; 'received:org': 0.37; 'release': 0.37; 'seem': 0.37; "won't": 0.38; 'means': 0.39; 'test': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'your': 0.60; 'skip:u 10': 0.61; 'replying': 0.61; 'cameron': 0.66; '100': 0.79; 'otten': 0.84; 'succeeds': 0.84 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd81db.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 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:100334 Ganesh Pal wrote: > On Thu, Dec 10, 2015 at 9:20 PM, Peter Otten <__peter__@web.de> wrote: >> Ganesh Pal wrote: >> > >> I recommend that you reread the unittest documentation. >> >> setUpClass() should be a class method, and if it succeeds you can release >> the ressources it required in the corresponding tearDownClass() method. >> As written the flags and the setUp()/tearDown() seem unnecessary. >> > > Thanks to peter , Cameron and Ben Finney , for replying to my various > question post . I needed a hint on the below > > > 1. If there is a setUpClass exception or failure , I don't want the > unittest to run ( I don't have teardown ) how do I handle this ? > The traceback on the console looks very bad it repeats for all > the test cases , that means if I have 100 testcases if setup fails . > I will get the failure for all the test cases > > #c_t.py > EEEE > ====================================================================== > ERROR: test01: test_01_inode_test > ---------------------------------------------------------------------- > Traceback (most recent call last): > File "c_t.py", line xx, in setUp > self.setupClass() > File "c_t.py", line xxx, in TestSetup > self.TestSetup() > File "c_t.py", line xx, in corruptSetup > sys.exit("/tmp is not mounted ...Exiting !!!") > SystemExit: /tmp is not mounted ...Exiting !!! > ====================================================================== > ERROR: test02 > ---------------------------------------------------------------------- > Traceback (most recent call last): > File "c_t.py", line 162, in test_02_hardlink_test > self.inject_failures['test02'])) > KeyError: 'test02' > > Ran 2 tests in 0.003s > FAILED (errors=2) Don't invoke sys.exit(), raise a meaningful exception instead. Then in setUpClass() you can catch the expected exceptions and raise a SkipTest. Example: $ cat mayfail.py import os import unittest def setup_that_may_fail(): if "FAIL" in os.environ: 1/0 class MyTests(unittest.TestCase): @classmethod def setUpClass(cls): try: setup_that_may_fail() # placeholder for your actual setup except Exception as err: raise unittest.SkipTest( "class setup failed") # todo: better message def test_one(self): pass def test_two(self): pass if __name__ == "__main__": unittest.main() When the setup succeeds: $ python mayfail.py .. ---------------------------------------------------------------------- Ran 2 tests in 0.000s OK Now let's pretend a failure by setting the FAIL environment variable. (In your actual code you won't do that as you get a "real" failure) $ FAIL=1 python mayfail.py s ---------------------------------------------------------------------- Ran 0 tests in 0.000s OK (skipped=1) There's one oddity with this approach -- only one skipped test is reported even though there are two tests in the class, neither of which is run.