John Hamlett IV
John Hamlett IV

Reputation: 495

Mock os.path.dirname using path

I am writing tests for a Python program that read Excel files.

In order to have a working test standard, I have what are called "Working_sheets".

My project structure is

Root/
--->tests
------->golden_sheets/
----------->Desired results
------->src
----------->Python Files
------->Working sheets/
---->main/
---->sheets/

The test I am running is

@patch('os.path.dirname')
    def test_ResolutionSLA_Full(self):
        datetime.date = MockDate
        print(datetime.date.today())
        os.path.dirname.return_value = "../working_sheets"
        resolutionSLA = ResolutionSLA()
        resolutionSLA.run_report()

However, I get the error

Traceback (most recent call last):
  File "/usr/lib/python3.5/unittest/case.py", line 59, in testPartExecutor
    yield
  File "/usr/lib/python3.5/unittest/case.py", line 601, in run
    testMethod()
  File "/usr/local/lib/python3.5/dist-packages/mock/mock.py", line 1305, in patched
    return func(*args, **keywargs)
TypeError: test_ResolutionSLA_Full() takes 1 positional argument but 2 were given

The way "dirname" is used is

self.dir_path = os.path.dirname(os.path.abspath(__file__)) + "/"

This usually returns

/home/jphamlett/Documents/Work/ServiceNowReportAutomation/

However, during the tests I want it to return

../working_sheets/

I even tried to use the same technique as datetime

import os


class MockOSPath(os.path):

    @classmethod
    def abspath(cls, file):
        return "../working_sheets/MockOSPath.py"

but this gives me the error

Traceback (most recent call last):
  File "/home/jphamlett/.jetbrain/pycharm/helpers/pycharm/_jb_unittest_runner.py", line 35, in <module>
    main(argv=args, module=None, testRunner=unittestpy.TeamcityTestRunner, buffer=not JB_DISABLE_BUFFERING)
  File "/usr/lib/python3.5/unittest/main.py", line 93, in __init__
    self.parseArgs(argv)
  File "/usr/lib/python3.5/unittest/main.py", line 140, in parseArgs
    self.createTests()
  File "/usr/lib/python3.5/unittest/main.py", line 147, in createTests
    self.module)
  File "/usr/lib/python3.5/unittest/loader.py", line 219, in loadTestsFromNames
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "/usr/lib/python3.5/unittest/loader.py", line 219, in <listcomp>
    suites = [self.loadTestsFromName(name, module) for name in names]
  File "/usr/lib/python3.5/unittest/loader.py", line 153, in loadTestsFromName
    module = __import__(module_name)
  File "/home/jphamlett/Documents/Work/ServiceNowReportAutomation/tests/src/ResolutionSLA_Tests.py", line 8, in <module>
    from MockOSPath import MockOSPath
  File "/home/jphamlett/Documents/Work/ServiceNowReportAutomation/tests/src/MockOSPath.py", line 4, in <module>
    class MockOSPath(os.path):
TypeError: module.__init__() takes at most 2 arguments (3 given)

What am I doing wrong? Mocking datetime works just fine.

Upvotes: 2

Views: 3923

Answers (1)

Enrique Saez
Enrique Saez

Reputation: 2700

As the error output of your test suggests you are passing two arguments to your test:

  • self: the test class
  • @patch('os.path.dirname'): the patched object

To fix it just add one more argument to your method signature and actually use it in your test:

@patch('os.path.dirname')
    def test_ResolutionSLA_Full(self, mock_dir):
        datetime.date = MockDate
        print(datetime.date.today())
        mock_dir.return_value = "../working_sheets"
        resolutionSLA = ResolutionSLA()
        resolutionSLA.run_report()

Upvotes: 1

Related Questions