Testing
When writing python katas, you might want to create modules that can be imported by the solution or tests.
This kata shows how to by manipulating sys.path
to allow importing from /home/codewarrior
folder and writing a python file there.
import sys
HOME_DIR = '/home/codewarrior'
def make_module(module_name):
if HOME_DIR not in sys.path:
sys.path.append(HOME_DIR)
moduleContent = 'foo = lambda x: x+1'
with open('{}/{}.py'.format(HOME_DIR, module_name), 'w') as moduleFile:
moduleFile.write(moduleContent)
Test.it('Module \'foobar\' should not initially exist')
try:
import foobar
test.expect(False, 'should not get here')
except Exception as e:
print(e)
Test.it('Calling the function')
make_module('foobar')
Test.it('Should now succeed importing the module')
import foobar
Test.it('foobar.foo(x) should increase it\'s argument by 1')
test.assert_equals(foobar.foo(3), 4)
test.assert_equals(foobar.foo(4), 5)