I have a discrepancy in understand of the basic unittest methods in python. Given this test file below:
import unittest, sys
class TestStringMethods(unittest.TestCase):
def setUp(self):
self.mystring = "example string"
def tearDown(self):
del self.mystring
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
with self.assertRaises(TypeError):
s.split(2)
My understanding based on what I've read ("Methods named setUp in a TestCase are run, automatically, before every test method.", http://ift.tt/1Jw9z3p, etc) I interpret the order of events like:
1. set up self.mystring
2. run test_upper
3. tear down self.mystring
4. set up self.mystring
5. run test_isupper
6. tear down self.mystring
7. set up self.mystring
8. run test_split
9. tear down self.mystring
My co-workers interpret the docs as saying unittest works as follows:
1. set up self.mystring
2. run test_upper
3. run test_isupper
4. run test_split
5. tear down self.mystring
this is a pretty important distinction, which one is right?
You are correct, the setUp and tearDown are called every single time. You can easily test that by running a print statement in setup and teardown and each test then see for yourself in the final output.
RépondreSupprimer