Created
June 27, 2018 12:54
-
-
Save Lavode/f7db5e8dd51c3df22468a308ebf748b6 to your computer and use it in GitHub Desktop.
- Run tests, from main folder, via `python -m unittest`; test files have to start with `test_` prefix, and be located in `test/` folder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Cube: | |
def __init__(self, length): | |
self.length = length | |
def surface_area(self): | |
return 6 * self.length ** 2 | |
def volume(self): | |
return self.length ** 3 | |
def shrink(self, delta): | |
if self.length - delta < 0: | |
raise ValueError('Length must be >= 0') | |
self.length -= delta | |
def grow(self, delta): | |
self.length += delta |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
from cube import Cube | |
class TestCube(unittest.TestCase): | |
def setUp(self): | |
self.cube = Cube(3) | |
def test_surface_area_calculation(self): | |
self.assertEqual(self.cube.surface_area(), 54) | |
def test_volume_calculation(self): | |
self.assertEqual(self.cube.volume(), 27) | |
def test_shrink_decreases_length(self): | |
self.cube.shrink(2) | |
self.assertEqual(self.cube.length, 1) | |
def test_grow_increases_length(self): | |
self.cube.grow(4) | |
self.assertEqual(self.cube.length, 7) | |
def test_shrink_throws_exception_if_negative_length(self): | |
with self.assertRaises(ValueError): | |
self.cube.shrink(4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment