0

The following is my directory structure

data
|__data.csv

core
|__module.py

unittests
|__core
   |__module_test.py

module.py has 2 functions, read_file(), which reads a file and add(a, b) that returns the sum of the 2 given numbers.

module.py:

import pandas

def readfile(file_path, file_name):
    df = pandas.read_csv(r"" +file_path + file_name + ".csv")
    return df

def add(a, b):
    return a + b

orig_df = readfile("..\data\\", "data.csv")
# do other things

module_test.py, has a test method for add

module_test.py:

import unittest
import core.module as module


class MyTestCase(unittest.TestCase):
    def test_add(self):
        x = module.add(1, 2)
        self.assertEqual(2, x)

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

However, when I run the test, I see in the traceback that the code in read_file() function is being executed and fails because it can't find the data file.

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Edu 2019.3.1\plugins\python-ce\helpers\pycharm\_jb_unittest_runner.py", line 35, in <module>
    main(argv=args, module=None, testRunner=unittestpy.TeamcityTestRunner, buffer=not JB_DISABLE_BUFFERING)
  File "C:\Users\bhats\Anaconda3\lib\unittest\main.py", line 100, in __init__
    self.parseArgs(argv)
  File "C:\Users\bhats\Anaconda3\lib\unittest\main.py", line 147, in parseArgs
    self.createTests()
  File "C:\Users\bhats\Anaconda3\lib\unittest\main.py", line 159, in createTests
    self.module)
  File "C:\Users\bhats\Anaconda3\lib\unittest\loader.py", line 220, in loadTestsFromNames
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "C:\Users\bhats\Anaconda3\lib\unittest\loader.py", line 220, in <listcomp>
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "C:\Users\bhats\Anaconda3\lib\unittest\loader.py", line 154, in loadTestsFromName
    module = __import__(module_name)
  File "C:\Users\bhats\PycharmProjects\alpha-hunts\unittests\core\module_test.py", line 2, in <module>
    import core.module as module
  File "C:\Users\bhats\PycharmProjects\alpha-hunts\core\module.py", line 10, in <module>
    orig_df = readfile("..\data\\", "data.csv")
  File "C:\Users\bhats\PycharmProjects\alpha-hunts\core\module.py", line 4, in readfile
    df = pandas.read_csv(r"" +file_path + file_name + ".csv")
  File "C:\Users\bhats\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 685, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "C:\Users\bhats\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 457, in _read
    parser = TextFileReader(fp_or_buf, **kwds)
  File "C:\Users\bhats\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 895, in __init__
    self._make_engine(self.engine)
  File "C:\Users\bhats\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 1135, in _make_engine
    self._engine = CParserWrapper(self.f, **self.options)
  File "C:\Users\bhats\Anaconda3\lib\site-packages\pandas\io\parsers.py", line 1917, in __init__
    self._reader = parsers.TextReader(src, **kwds)
  File "pandas\_libs\parsers.pyx", line 382, in pandas._libs.parsers.TextReader.__cinit__
  File "pandas\_libs\parsers.pyx", line 689, in pandas._libs.parsers.TextReader._setup_parser_source
FileNotFoundError: [Errno 2] File b'..\\data\\data.csv.csv' does not exist: b'..\\data\\data.csv.csv'

Why is this happening ? If I individually import the function, this does not happen however, I noticed. Can someone explain why this is happening please ?

SuB
  • 25
  • 4

1 Answers1

0

When you importing a module, Python runs the code.

On module scope you have:

# 1. imported a module:
import pandas

# 2. defined two functions:
def readfile(file_path, file_name):
def add(a, b):

# 3. and at the end run one of them 
orig_df = readfile("..\data\\", "data.csv")

The last line of your tested file is invoking readfile on import.

Easiest way is to move that code to a function or to a different module.

If you need to run some code as a script you can use if __name__ == "__main__": (same as in your testcase)

if __name__ == "__main__":
    # The code here will run only if the module is executed
    # as a script, eg:
    #    python module.py
    orig_df = readfile("..\data\\", "data.csv")
    # do other things

What does if __name__ == "__main__": do?

kwarunek
  • 12,141
  • 4
  • 43
  • 48
  • Are you saying importing the file in the test file is equivalent to running it ? – SuB Apr 27 '20 at 04:33
  • I suppose you are saying just keep the functions in the module and call them from else where ? That does make sense.. let me know if I understand it right please ! – SuB Apr 27 '20 at 04:44