数学方程的单元测试
Unit tests for math equations
我需要为 a= (b+c)* d 和布尔值等数学方程式生成单元测试 expressions.Is 有什么方法 and/or 实用程序可以实现这一点?
注:
由于问题的模糊性,我假设可以使用的编程语言是可变的。在这种情况下 Python 使用面向对象的方法进行单元测试。
答案:
对于 python 中的大多数单元测试问题,您可以使用标准单元测试库。给定方程式的示例可以是:
import unittest
def test_equation(b,c,d):
return (b+c) * d
class testEquation(unittest.TestCase):
def setUp(self):
pass
def testEquationInput(self):
b = 5
c = 4
d = 10
self.assertEqual(test_equation(b,c,d), 90)
if __name__ == "__main__":
unittest.main()
您可以使用以下代码执行此代码:
python unittest test_module.py
或者如果你用单元测试写一个目录:
python -m unittest discover /path/to/test/directory
我需要为 a= (b+c)* d 和布尔值等数学方程式生成单元测试 expressions.Is 有什么方法 and/or 实用程序可以实现这一点?
注:
由于问题的模糊性,我假设可以使用的编程语言是可变的。在这种情况下 Python 使用面向对象的方法进行单元测试。
答案:
对于 python 中的大多数单元测试问题,您可以使用标准单元测试库。给定方程式的示例可以是:
import unittest
def test_equation(b,c,d):
return (b+c) * d
class testEquation(unittest.TestCase):
def setUp(self):
pass
def testEquationInput(self):
b = 5
c = 4
d = 10
self.assertEqual(test_equation(b,c,d), 90)
if __name__ == "__main__":
unittest.main()
您可以使用以下代码执行此代码:
python unittest test_module.py
或者如果你用单元测试写一个目录:
python -m unittest discover /path/to/test/directory