在函数内部创建 Unittest class 不起作用
Creating Unittest class inside function not working
在附加的脚本中,为什么 0 个测试用例是 运行
import unittest
def smg():
def add(x, y):
return x + y
class SimpleTest(unittest.TestCase):
def testadd1(self):
self.assertEquals(add(4, 5), 9)
if __name__ == '__main__':
unittest.main()
smg()
给予
Ran 0 tests in 0.000s
有什么办法可以解决,请协助
取出 smg
函数中的代码,然后使用 python -m unittest <Your Filename>.py
.
从命令行进行 运行 测试
您的代码将如下所示:
import unittest
def add(x, y):
return x + y
class SimpleTest(unittest.TestCase):
def testadd1(self):
self.assertEquals(add(4, 5), 9)
if __name__ == '__main__':
unittest.main()
此外,您可能会收到 assertEquals
的弃用警告。您可能想将其更改为 assertEqual
。
您可能对 unittest.TextTestRunner 感兴趣:
A basic test runner implementation that outputs results to a stream.
样本usage:
However, should you want to customize the building of your test suite, you can do it yourself:
def suite():
suite = unittest.TestSuite()
suite.addTest(WidgetTestCase('test_default_widget_size'))
suite.addTest(WidgetTestCase('test_widget_resize'))
return suite
if __name__ == '__main__':
runner = unittest.TextTestRunner()
runner.run(suite())
针对您的情况的示例 运行。
src.py
def add(x, y):
print("Add", x, y)
return x + y
test_src.py
import unittest
from src import add
class SimpleTest(unittest.TestCase):
def testadd1(self):
self.assertEqual(add(4, 5), 9)
if __name__ == '__main__':
unittest.main()
运行 测试正常
$ python test_src.py # Using unittest
Add 4 5
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
$ pytest -q # Using pytest
.
1 passed in 0.06s
现在如果你想通过函数手动调用它。
run_tests.py
import unittest
import test_src
def suite():
suite = unittest.TestSuite()
suite.addTest(test_src.SimpleTest('testadd1'))
return suite
def run():
runner = unittest.TextTestRunner()
runner.run(suite())
# run() # Uncomment if you want to try to run it as a script e.g. <python run_tests.py>
您现在只需导入文件并在需要时调用 run()
:
$ python3
>>> import run_tests
>>> run_tests.run()
Add 4 5
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
>>>
在附加的脚本中,为什么 0 个测试用例是 运行
import unittest
def smg():
def add(x, y):
return x + y
class SimpleTest(unittest.TestCase):
def testadd1(self):
self.assertEquals(add(4, 5), 9)
if __name__ == '__main__':
unittest.main()
smg()
给予
Ran 0 tests in 0.000s
有什么办法可以解决,请协助
取出 smg
函数中的代码,然后使用 python -m unittest <Your Filename>.py
.
您的代码将如下所示:
import unittest
def add(x, y):
return x + y
class SimpleTest(unittest.TestCase):
def testadd1(self):
self.assertEquals(add(4, 5), 9)
if __name__ == '__main__':
unittest.main()
此外,您可能会收到 assertEquals
的弃用警告。您可能想将其更改为 assertEqual
。
您可能对 unittest.TextTestRunner 感兴趣:
A basic test runner implementation that outputs results to a stream.
样本usage:
However, should you want to customize the building of your test suite, you can do it yourself:
def suite(): suite = unittest.TestSuite() suite.addTest(WidgetTestCase('test_default_widget_size')) suite.addTest(WidgetTestCase('test_widget_resize')) return suite if __name__ == '__main__': runner = unittest.TextTestRunner() runner.run(suite())
针对您的情况的示例 运行。
src.py
def add(x, y):
print("Add", x, y)
return x + y
test_src.py
import unittest
from src import add
class SimpleTest(unittest.TestCase):
def testadd1(self):
self.assertEqual(add(4, 5), 9)
if __name__ == '__main__':
unittest.main()
运行 测试正常
$ python test_src.py # Using unittest
Add 4 5
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
$ pytest -q # Using pytest
.
1 passed in 0.06s
现在如果你想通过函数手动调用它。
run_tests.py
import unittest
import test_src
def suite():
suite = unittest.TestSuite()
suite.addTest(test_src.SimpleTest('testadd1'))
return suite
def run():
runner = unittest.TextTestRunner()
runner.run(suite())
# run() # Uncomment if you want to try to run it as a script e.g. <python run_tests.py>
您现在只需导入文件并在需要时调用 run()
:
$ python3
>>> import run_tests
>>> run_tests.run()
Add 4 5
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
>>>