UnitTest break on failure

2009-02-10 Thread Qian Xu
Hi All,

i am writing unit tests and have got a problem:
I want to test some code sequence like:

 self.assertEquals(testMethod1(), expected_value1);
 self.assertEquals(testMethod2(), expected_value2);

However, if the first test item is failed, no more tests will be executed.
Can I tell Python, 
1. go ahead, if a failure is occurred. 
2. stop, if an error is occurred?


Best regards,
Qian Xu
--
http://mail.python.org/mailman/listinfo/python-list


Re: UnitTest break on failure

2009-02-10 Thread Joe Riopel
On Tue, Feb 10, 2009 at 11:48 AM, Qian Xu quian...@stud.tu-ilmenau.de wrote:
  self.assertEquals(testMethod1(), expected_value1);
  self.assertEquals(testMethod2(), expected_value2);

 However, if the first test item is failed, no more tests will be executed.
 Can I tell Python,
 1. go ahead, if a failure is occurred.
 2. stop, if an error is occurred?

Typically you would have a different test for two different methods,
one for each method.
--
http://mail.python.org/mailman/listinfo/python-list


Re: UnitTest break on failure

2009-02-10 Thread Scott David Daniels

Qian Xu wrote:

i am writing unit tests and have got a problem:
I want to test some code sequence like:

 self.assertEquals(testMethod1(), expected_value1);
 self.assertEquals(testMethod2(), expected_value2);

However, if the first test item is failed, no more tests will be executed.
Can I tell Python, 
1. go ahead, if a failure is occurred. 
2. stop, if an error is occurred?


Well, each test should be independent, so generally you are talking
about multiple tests.  If you really want a test to see if you get
through a sequence in a single test:
import unittest
...
class MyTest(unittest.TestCase):

def test_multi_step(self):
expectations = [(expected_value1, testMethod1),
(expected_value2, testMethod2),
(final_expected, another_test, arg1, arg2)]
try:
for step, ops in enumerate(expectations):
self.assertEquals(ops[0], ops[1](*ops[2:]))
except Exception, why:
raise ValueError('Step %s Failed: %s' % (step, why))
# here the test passed.
...
if __name__ == '__main__':
unittest.main()

Note this gives you less information (the traceback doesn't go down
to the actual error), but it shows you what step failed.

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list