'How to fetch the status of test-case in teardown, after each test-case completion (pyunit)?
Executing few test-cases as part of test-suite. Goal is to skip test-case, if there is any failure in previous test-cases or any dependency.
class New():
global status
status = []
def setUp(self):
self.browser = None
def test_1(self):
print("1")
def test_2(self):
print(2)
assert False
def test_3(self):
if 'test_2' in status:
self.skipTest("test_2 FAILED")
print(3)
# assert False
def test_4(self):
if any(x in ['test_2', 'test_3'] for x in status):
self.skipTest("test_2 or test_3 FAILED")
print(4)
def test_5(self):
print(5)
def list2reason(self, exc_list):
print("EXEC LIST : {}".format(exc_list))
if exc_list and exc_list[-1][0] is self:
print(exc_list[-1][0])
return exc_list[-1][1]
def tearDown(self):
if self.browser:
super().tearDown(close_browser=False)
self.browser.quit()
if hasattr(self, '_outcome'): # Python 3.4+
result = self.defaultTestResult() # these 2 methods have no side effects
self._feedErrorsToResult(result, self._outcome.errors)
else: # Python 3.2 - 3.3 or 2.7
result = getattr(self, '_outcomeForDoCleanups', self._resultForDoCleanups)
error = self.list2reason(result.errors)
failure = self.list2reason(result.failures)
skipped = self.list2reason(result.skipped)
print("error : {} , failure : {}, skipped: {}".format(error, failure, skipped))
ok = not error and not failure
# demo: report short info immediately (not important)
if not ok:
status.append(self._testMethodName)
typ, text = ('ERROR', error) if error else ('FAIL', failure)
msg = [x for x in text.split('\n')[1:] if not x.startswith(' ')][0]
print("\n== tearDown check: %s: %s\n %s" % (typ, self.id(), msg))
However, I am unable to capture the skipped status. It always shows None, though test-cases skipped. And skipIf(condition) doesn't seem to be working. So, using SkipTest inside test. Am i missing anything here ?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
