python 调用其他方法的单元测试方法
python unittest methods which call other methods
实施基本 space 几何 class
Class Geometry(object):
# radius is required and must be float
def circleSurface(self, radius):
if not isinstance(radius, float):
raise AttributeError("best to enter a float as radius")
else:
return radius*radius*3.14
# height and radius are required and must be floats
def cylinderVolume(self, radius, height):
if not isinstance(height, float):
raise AttributeError("best to enter a float as height")
else:
return self.circleSurface(radius) * height
并编写一个基本的几何图形class测试器
import unittest
import Geometry
class GeometryTest(unittest.TestCase):
def test_circleSurfaceRaisesAttributeErrorWhenRadiusIsNotFloat(self):
with self.assertRaises(AttributeError) as raised_exception:
Geometry.circleSurface(radius = "B")
self.assertEqual(str(raised_exception.exception), "best to enter a float as radius")
def test_cylinderVolumeRaisesAttributeErrorWhenHeightIsNotFloat(self):
with self.assertRaises(AttributeError) as raised_exception:
Geometry.cylinderVolume(radius = 1.0, height = "B")
self.assertEqual(str(raised_exception.exception), "best to enter a float as height")
似乎可以做到,但我也想在 cylinderVolume 方法中测试半径是否为浮点数,即使它是调用 circleSurface 方法时固有的:
def test_cylinderVolumeRaisesAttributeErrorWhenRadiusIsNotFloat(self):
with self.assertRaises(AttributeError) as raised_exception:
Geometry.cylinderVolume(radius = "B", height = 1.0)
self.assertEqual(str(raised_exception.exception), "best to enter a float as radius")
测试调用 cylinderVolume 的所有参数感觉很自然。
我知道添加最后一个测试不会导致任何 'overhead' 或大量时间消耗,您不妨这样做,但另一方面它似乎有点多余、矫枉过正和重复代码。只是想知道是否有解决此类测试的特定标准。
cylinderVolume
调用 circleSurface
是实现细节。您知道 radius
的验证发生在 cylinderVolume
现在 但在重构之后这可能会改变。如果你想确保不会出现这种回归,你应该测试 radius
对 cylinderVolume
.
的验证
实施基本 space 几何 class
Class Geometry(object):
# radius is required and must be float
def circleSurface(self, radius):
if not isinstance(radius, float):
raise AttributeError("best to enter a float as radius")
else:
return radius*radius*3.14
# height and radius are required and must be floats
def cylinderVolume(self, radius, height):
if not isinstance(height, float):
raise AttributeError("best to enter a float as height")
else:
return self.circleSurface(radius) * height
并编写一个基本的几何图形class测试器
import unittest
import Geometry
class GeometryTest(unittest.TestCase):
def test_circleSurfaceRaisesAttributeErrorWhenRadiusIsNotFloat(self):
with self.assertRaises(AttributeError) as raised_exception:
Geometry.circleSurface(radius = "B")
self.assertEqual(str(raised_exception.exception), "best to enter a float as radius")
def test_cylinderVolumeRaisesAttributeErrorWhenHeightIsNotFloat(self):
with self.assertRaises(AttributeError) as raised_exception:
Geometry.cylinderVolume(radius = 1.0, height = "B")
self.assertEqual(str(raised_exception.exception), "best to enter a float as height")
似乎可以做到,但我也想在 cylinderVolume 方法中测试半径是否为浮点数,即使它是调用 circleSurface 方法时固有的:
def test_cylinderVolumeRaisesAttributeErrorWhenRadiusIsNotFloat(self):
with self.assertRaises(AttributeError) as raised_exception:
Geometry.cylinderVolume(radius = "B", height = 1.0)
self.assertEqual(str(raised_exception.exception), "best to enter a float as radius")
测试调用 cylinderVolume 的所有参数感觉很自然。
我知道添加最后一个测试不会导致任何 'overhead' 或大量时间消耗,您不妨这样做,但另一方面它似乎有点多余、矫枉过正和重复代码。只是想知道是否有解决此类测试的特定标准。
cylinderVolume
调用 circleSurface
是实现细节。您知道 radius
的验证发生在 cylinderVolume
现在 但在重构之后这可能会改变。如果你想确保不会出现这种回归,你应该测试 radius
对 cylinderVolume
.