@JaceBrowning
def times_2_plus_1(arg1):
result = _expensive_x2(arg1) + 1
return result
import time
def _expensive_x2(arg1):
time.sleep(3) # expensive math, IO, 3rd-party API, etc.
return arg1 * 2
times_2_plus_1(7)
15
import unittest
class TestMath(unittest.TestCase):
def test_math(self):
self.assertEquals(15, times_2_plus_1(7))
suite = unittest.TestLoader().loadTestsFromTestCase(TestMath)
unittest.TextTestRunner(verbosity=2).run(suite)
<unittest.runner.TextTestResult run=1 errors=0 failures=0>
import unittest
import mock
class TestMath2(unittest.TestCase):
@mock.patch('__main__._expensive_x2', mock.Mock(return_value=14))
def test_math(self):
self.assertEquals(15, times_2_plus_1(7))
suite = unittest.TestLoader().loadTestsFromTestCase(TestMath2)
unittest.TextTestRunner(verbosity=2).run(suite)
<unittest.runner.TextTestResult run=1 errors=0 failures=0>
import unittest
import mock
import os
ENV = 'TEST_INTEGRATION'
REASON = "'{0}' not set".format(ENV)
class TestMath3(unittest.TestCase):
@mock.patch('__main__._expensive_x2', mock.Mock(return_value=14))
def test_math(self):
self.assertEquals(15, times_2_plus_1(7))
@unittest.skipUnless(os.getenv(ENV), REASON)
def test_math_integration(self):
self.assertEquals(15, times_2_plus_1(7))
suite = unittest.TestLoader().loadTestsFromTestCase(TestMath3)
results = unittest.TextTestRunner(verbosity=2).run(suite)
print results
print results.skipped
<unittest.runner.TextTestResult run=2 errors=0 failures=0> []
import os
os.environ['TEST_INTEGRATION'] = ''
import os
os.environ['TEST_INTEGRATION'] = '1'
package/
__init__.py
module1.py
module2.py
test/
__init__.py # integration tests for the package
test_module1.py # unit and integration tests for the module
test_module2.py # unit and integraiton tests for the module
Makefile:
...
.PHONY test
test:
nosetests
.PHONY tests
tests:
TEST_INTEGRATION=1 nosetests --verbose
...