0

I have encountered this error and tried a number of fixes posted on this site but have had no luck. I wrote a unit test and cannot call it due to the Start Directory Not Importable error. My file structure is:

Root
    └── tests
        └── fixtures
            └── customFile.json
        ├── __init__.py
        └── functionTest.py
    └── Lambdas
        └── FunctionA
            ├── __init__.py
            └── FunctionToTest.py

and my unit test looks like this:

import unittest
import json
import boto3

from Lambdas.FunctionA import FunctionToTest

s3 = boto3.client('s3', 'us-east-1')
file = s3.get_object(Bucket="BUCKET", Key="KEY")

mapper = s3.get_object(Key="KEY", Bucket="BUCKET")
mapperJSON = json.loads(mapper['Body'].read().decode('utf-8'))

path = r"./tests/fixtures/customFile.json"
with open(path) as file:
    endList = json.load(file)
    file.close()

class TestMatchModeler(unittest.TestCase):
    def test_run(self):
        test = FunctionToTest(file, mapperJSON)
        self.assertEqual(len(test), len(endList))


if __name__ == '__main__':
    unittest.main()

and the actual code it's testing is:

def FunctionToTest(file, mapperjson):
    Fields = mapperjson['mappings']

    lines1 = file['Body'].read().decode('utf-8').split('\n')
    fieldnames = lines1[0].replace('"','').split(',')
    testls = [row for row in csv.DictReader(lines1[1:], fieldnames)]
    out = json.dumps(testls)
    jlist1 = json.loads(out)

    dicts = []
    Fieldsinv = {v: k for k, v in Fields.items()}
    for i in jlist1:
        d = {}
        for k, v in i.items():
            if k in Fieldsinv:
                d[Fieldsinv[k]] = v
        d['individual_id'] = str(uuid.uuid4())
        dicts.append(d)
    return dicts

Essentially the customFile.json is a list of dictionaries that I know are correct, and I want to test that my code modifies a CSV correctly to have the same number of dicts. This question has been asked a lot and the answer is always to have the files linked via an init.py file but as you can see I've done that and it still doesn't work.

I'm sure this may get flagged as a duplicate but im not sure what else to try.

DBA108642
  • 1,995
  • 1
  • 18
  • 55

2 Answers2

0

It looks like you are missing the __init__.py files in some directories: root, tests and lambdas.

See here: python - unittest - ImportError: Start directory is not importable

Artq99
  • 1
  • Hi @Artq99, I have added init files to each of those directories and I still get the same error unfortunately – DBA108642 Jul 15 '20 at 12:42
0

turns out running this command

python -m tests.functionTest

did the trick

DBA108642
  • 1,995
  • 1
  • 18
  • 55