@ La respuesta de dmvianna me dio la posibilidad de ejecutar unittest
en un cuaderno jupyter (ipython), pero tuve que hacer un poco más. Si escribiera simplemente lo siguiente:
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)
suite = unittest.TestLoader().loadTestsFromModule (TestStringMethods)
unittest.TextTestRunner().run(suite)
tengo
Ran 0 tests in 0.000s
OK
No está rota, pero no se ejecuta ninguna prueba! Si crea una instancia de la clase de prueba
suite = unittest.TestLoader().loadTestsFromModule (TestStringMethods())
(tenga en cuenta los parens al final de la línea, que es el único cambio) Tengo
ValueError Traceback (most recent call last) in() ----> 1 suite = unittest.TestLoader().loadTestsFromModule (TestStringMethods())
/usr/lib/python2.7/unittest/case.pyc in init(self, methodName) 189 except AttributeError: 190 raise ValueError("no such test method in %s: %s" % --> 191 (self.class, methodName)) 192 self._testMethodDoc = testMethod.doc 193 self._cleanups = []
ValueError: no such test method in : runTest
La solución es ahora bastante claro: poner runTest
a la prueba clase:
class TestStringMethods(unittest.TestCase):
def runTest(self):
test_upper (self)
test_isupper (self)
test_split (self)
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)
suite = unittest.TestLoader().loadTestsFromModule (TestStringMethods())
unittest.TextTestRunner().run(suite)
Ran 3 tests in 0.002s
OK
también funciona correctamente (y corre 3 pruebas) si mi runTest
solo pass
es, según lo sugerido por @Darren.
Esto es un poco yucchy, lo que requiere un poco de trabajo manual de mi parte, pero también es más explícita , y eso es una virtud Python, ¿verdad?
No pude obtener ninguna de las técnicas llamando al unittest.main
con argumentos explícitos desde aquí o desde esta pregunta relacionada Unable to run unittest's main function in ipython/jupyter notebook para trabajar dentro de un portátil Jupyter, pero estoy de regreso en la carretera con un tanque lleno de gasolina.
¿Cómo responde la pregunta? ¿Preguntó sobre la virtud de una buena cobertura? ¿Por qué no dicen sobre cómo "jugar según sus reglas"? "¡magia negra profunda" de verdad !? – saaj
Desplácese hacia abajo, esta es la respuesta menos útil con diferencia. – jwg