0

That is my project/source directory. I am using unittest and trying to run these tests from bash: 'python -m unittest'. I have tried many different things from using os and adding it to syspath and different similar approaches, also tried many different options of importing itself like 'from algorithms import BubbleSort', or 'from algorithms.BubbleSort import BubbleSort'. Saw this for example but it didn't help either.

enter image description here

# inside MergeSort file
class MergeSort:
    def sort_asc(self, list):
        return sorted_list

# inside BubbleSort file
class BubbleSort:
    def sort_asc(self, list):
        return sorted_list

# inside test_main
import unittest

from algorithms.MergeSort import MergeSort
from algorithms.BubbleSort import BubbleSort
from utility.Utils import Utils


class test_algorithms(unittest.TestCase):
    # and here are all my test functions
    def test_bubble_asc_empty(self):
        numbers = []
        self.assertListEqual(BubbleSort().sort_asc(numbers), [])

Files BubbleSort and MergeSort both contain 1 class called same as the filename. Inside the application view files from eg. controllers directory can normally access algorithms directory files by importing algorithms.BubbleSort but this test_main.py just can't do same thing for some reason and all my attempts end up having either this 'is not defined error', 'module object is not callable error' or 'failed to import module error'. What am I doing wrong here?

enter image description here

  • @ReblochonMasque it changes my error to TypeError: 'module' object is not callable – stiwenparker Jan 21 '22 at 12:45
  • Please provide a minimal reproducible example so we can see what's happening. – tlgs Jan 21 '22 at 13:26
  • If you use `import algorithms.BubbleSort` then you should reference the class with `BubbleSort.BubbleSort` (`module_name.class_name`). – tlgs Jan 21 '22 at 13:28
  • @tlgs i added some shape of the code i have in my files. – stiwenparker Jan 21 '22 at 13:36
  • I realised now that if I call my tests using python -m tests.test_main they don't have any problems with importing but if i call python -m unittest (like internet told me to) it has these problems with importing (pycharm also won't run it) – stiwenparker Jan 21 '22 at 13:39
  • Is your module pip-installed? When I do something like this, I work with an editable install, via `pip install -e .` run where the top-level `setup.py` is, so that Python sees my module from anywhere. – joanis Jan 21 '22 at 13:41
  • Do you still have the `__init__.py` files under both `algorithms` and `tests`? – tlgs Jan 21 '22 at 13:41
  • See, for example, https://stackoverflow.com/a/69228512/3216427 – joanis Jan 21 '22 at 13:42

0 Answers0