I have to write a test file like this:
import unittest
from mylibrary import some_crazy_func
class TestSomething(unittest.TestCase):
def test_some_crazy_func_that_needs_io_open(self):
# Opens file
# Calls function
# assert outputs
But I'm unsure where is the "pythonic location" where I should import the library (let's say io
).
Should it be at the top:
import io
import unittest
from mylibrary import some_crazy_func
class TestSomething(unittest.TestCase):
def test_some_crazy_func_that_needs_io_open(self):
expected = ['abc', 'def', 'xyz']
with io.open('somestaticfile.txt', 'r') as fin:
outputs = [some_crazy_func(line) for line in fin]
assert outputs == expected
Or within the TestCase
's function:
import unittest
from mylibrary import some_crazy_func
class TestSomething(unittest.TestCase):
def test_some_crazy_func_that_needs_io_open(self):
import io
expected = ['abc', 'def', 'xyz']
with io.open('somestaticfile.txt', 'r') as fin:
outputs = [some_crazy_func(line) for line in fin]
assert outputs == expected
Or is it before the TestCase function and at the object initialization:
import unittest
from mylibrary import some_crazy_func
class TestSomething(unittest.TestCase):
import io
def test_some_crazy_func_that_needs_io_open(self):
expected = ['abc', 'def', 'xyz']
with io.open('somestaticfile.txt', 'r') as fin:
outputs = [some_crazy_func(line) for line in fin]
assert outputs == expected