unittest#
Última modificación: Mayo 14, 2022
[1]:
%%writefile example.py
import unittest
class TestStringMethods(unittest.TestCase):
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'])
# check that s.split fails when the separator is not a string
with self.assertRaises(TypeError):
s.split(2)
if __name__ == '__main__':
unittest.main()
Writing example.py
[2]:
!python3 example.py
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s
OK
Interfaz de línea de comandos#
[3]:
!python3 -m unittest example
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s
OK
[6]:
!python3 -m unittest -v example
test_isupper (example.TestStringMethods) ... ok
test_split (example.TestStringMethods) ... ok
test_upper (example.TestStringMethods) ... ok
----------------------------------------------------------------------
Ran 3 tests in 0.001s
OK
[4]:
!python3 -m unittest example.TestStringMethods
...
----------------------------------------------------------------------
Ran 3 tests in 0.000s
OK
[5]:
!python3 -m unittest example.TestStringMethods.test_split
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
Test discovery#
[7]:
!python3 -m unittest discover
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
Organización del código de prueba#
[8]:
#
# Los tests son funciones que empiezan por 'test'.
#
import unittest
class DefaultWidgetSizeTestCase(unittest.TestCase):
def test_default_widget_size(self):
widget = Widget('The widget')
self.assertEqual(widget.size(), (50, 50))
[13]:
%%writefile example.py
import unittest
class TestStringMethods(unittest.TestCase):
def setUp(self):
#
# Esta función se ejecuta a la entrada de cada test
#
print('in setUp()')
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
print('in test_upper()')
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
print('in test_isupper()')
def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
with self.assertRaises(TypeError):
s.split(2)
print('in test_split()')
def tearDown(self):
#
# Esta función se ejecuta a la salida de cada test
#
print('in tearDown()')
Overwriting example.py
[14]:
!python3 -m unittest example.py
in setUp()
in test_isupper()
in tearDown()
.in setUp()
in test_split()
in tearDown()
.in setUp()
in test_upper()
in tearDown()
.
----------------------------------------------------------------------
Ran 3 tests in 0.000s
OK
Descarte de tests y fallas#
[19]:
%%writefile example.py
import unittest
class TestStringMethods(unittest.TestCase):
def setUp(self):
#
# Esta función se ejecuta a la entrada de cada test
#
print('in setUp()')
@unittest.skip("skipping test_upper()")
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
print('in test_upper()')
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
print('in test_isupper()')
def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
with self.assertRaises(TypeError):
s.split(2)
print('in test_split()')
def tearDown(self):
#
# Esta función se ejecuta a la salida de cada test
#
print('in tearDown()')
Overwriting example.py
[21]:
!python3 -m unittest -v example.py
test_isupper (example.TestStringMethods) ... in setUp()
in test_isupper()
in tearDown()
ok
test_split (example.TestStringMethods) ... in setUp()
in test_split()
in tearDown()
ok
test_upper (example.TestStringMethods) ... skipped 'skipping test_upper()'
----------------------------------------------------------------------
Ran 3 tests in 0.001s
OK (skipped=1)
[22]:
%%writefile example.py
import unittest
@unittest.skip("skipping class()")
class TestStringMethods(unittest.TestCase):
def setUp(self):
#
# Esta función se ejecuta a la entrada de cada test
#
print('in setUp()')
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')
print('in test_upper()')
def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
print('in test_isupper()')
def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
with self.assertRaises(TypeError):
s.split(2)
print('in test_split()')
def tearDown(self):
#
# Esta función se ejecuta a la salida de cada test
#
print('in tearDown()')
Overwriting example.py
[23]:
!python3 -m unittest -v example.py
test_isupper (example.TestStringMethods) ... skipped 'skipping class()'
test_split (example.TestStringMethods) ... skipped 'skipping class()'
test_upper (example.TestStringMethods) ... skipped 'skipping class()'
----------------------------------------------------------------------
Ran 3 tests in 0.000s
OK (skipped=3)
[24]:
%%writefile example.py
import unittest
class ExpectedFailureTestCase(unittest.TestCase):
@unittest.expectedFailure
def test_fail(self):
self.assertEqual(1, 0, "broken")
Overwriting example.py
[25]:
!python3 -m unittest -v example.py
test_fail (example.ExpectedFailureTestCase) ... expected failure
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK (expected failures=1)
@unittest.skip(reason)
@unittest.skipIf(condidtion, reason)
@unittest.skipUnless(condidtion, reason)
@unittest.expectedFailure
Métodos disponibles#
assertEqual(first, second, msg=None)
assertNotEqual(first, second, msg=None)
assertTrue(expr, msg=None)
assertFalse(expr, msg=None)
assertIs(first, second, msg=None)
assertIsNot(first, second, msg=None)
assertIsNone(expr, msg=None)
assertIsNotNone(expr, msg=None)
assertIn(member, container, msg=None)
assertNotIn(member, container, msg=None)
assertIsInstance(obj, cls, msg=None)
assertNotIsInstance(obj, cls, msg=None)
assertRaises(exception, callable, *args, **kwds)
assertRaises(exception, *, msg=None)
assertRaisesRegex(exception, regex, callable, *args, **kwds)
assertRaisesRegex(exception, regex, *, msg=None)
assertWarns(warning, callable, *args, **kwds)
assertWarns(warning, *, msg=None)
assertWarnsRegex(warning, regex, callable, *args, **kwds)
assertWarnsRegex(warning, regex, *, msg=None)
assertLogs(logger=None, level=None)
assertNoLogs(logger=None, level=None)
assertAlmostEqual(first, second, places=7, msg=None, delta=None)
assertNotAlmostEqual(first, second, places=7, msg=None, delta=None)
assertGreater(first, second, msg=None)
assertGreaterEqual(first, second, msg=None)
assertLess(first, second, msg=None)
assertLessEqual(first, second, msg=None)
assertRegex(text, regex, msg=None)
assertNotRegex(text, regex, msg=None)