【发布时间】:2019-01-06 08:20:39
【问题描述】:
在我的UnitTest 目录中,我有两个文件,mymath.py 和test_mymath.py。
mymath.py文件:
def add(a, b):
return a + b
def subtract(a, b):
return a - b
def multiply(a, b):
return a * b
def divide(numerator, denominator):
return float(numerator) / denominator
test_mymath.py 文件是:
import mymath
import unittest
class TestAdd(unittest.TestCase):
"""
Test the add function from the mymath library
"""
def test_add_integer(self):
"""
Test that the addition of two integers returns the correct total
"""
result = mymath.add(1, 2)
self.assertEqual(result, 3)
def test_add_floats(self):
"""
Test that the addition of two integers returns the correct total
"""
result = mymath.add(10.5, 2)
self.assertEqual(result, 12.5)
def test_add_strings(self):
"""
Test that the addition of two strings returns the two strings as one
concatenated string
"""
result = mymath.add('abc', 'def')
self.assertEqual(result, 'abcdef')
if __name__ == '__main__':
unittest.main()
当我运行命令时
python .\test_mymath.py
我得到了结果
在 0.000 秒内运行 3 次测试
好的
但是当我尝试使用
运行测试时python -m unittest .\test_mymath.py
我收到了错误
ValueError:空模块名称
我正在关注这个article
我的python版本是Python 3.6.6,我在本地机器上使用的是windows 10。
【问题讨论】:
-
请这样运行:
python -m unittest test_mymath