3. Test passes
ろ碁ゼ 糾骸 蟆 豕
襦蠏碁覦
Test fails Refactor
ろ ろ碁ゼ
貊 譟伎 蟇 覦 讌 レ
4. Basic Example
import random
import unittest
class PizzaTest(unittest.TestCase):
def setUp(self):
self.seq = list(range(10))
def test_shuffle(self):
unittest.TestCase襯 覦
random.shuffle(self.seq)
self.seq.sort()
self.assertEqual(self.seq, list(range(10)))
#襴ろ碁ゼ 覲蟆渚伎狩 覲蟆曙 TypeError 覦
self.assertRaises(TypeError, random.shuffle, (1,2,3))
def test_choice(self):
element = random.choice(self.seq)
self.assertTrue(element in self.seq)
def test_sample(self):
with self.assertRaises(ValueError):
#豌覯讌 朱誤郁 螳讌 襴ろ 螳 覲企 襷 襯 ロ ValueError螳 覦
random.sample(self.seq, 20)
for element in random.sample(self.seq, 5):
self.assertTrue(element in self.seq)
ろ ろ 襷 襾殊 ろ 譴觜
ろ 企
test襦 伎
$ python3 -m unittest tests.py
5. Test Discovery
ろ碁ゼ 谿城 伎 貉れろ磯伎 .
python -m unittest discover -s project_directory -p '*_test.py'
-v, --verbose
Verbose output
-s, --start-directory directory
Directory to start discovery (. default)
-p, --pattern pattern
Pattern to match test files (test*.py default)
-t, --top-level-directory directory
6. Test Suite
願 襦 ろ 蟲 蠍
def suite():
suite = unittest.TestSuite()
suite.addTest(PizzaTest('test_choice'))
# suite.addTest(PizzaTest('test_sample'))
return suite
if __name__ == '__main__':
unittest.TextTestRunner().run(suite())
7. Test Skip
@unittest.skip("demonstrating skipping")
def test_nothing(self):
self.fail("shouldn't happen")
@unittest.skipIf(mylib.__version__ < (1, 3),
"not supported in this library version")
def test_format(self):
# Tests that work for only a certain version of the library.
pass
@unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
def test_windows_support(self):
# windows specific testing code
pass
26. Request test
def test_test_view(self):
c = Client()
res = c.get('/test_view/', {})
e = {'result': 1}
self.assertJSONEqual(res.content.decode('utf-8'), json.dumps(e))
post, head, put, patch, delete, trace