2017-07-13 72 views
1

我正在爲讀取Excel文件的Python程序編寫測試。使用路徑模擬os.path.dirname

爲了有一個工作測試標準,我有所謂的「工作表」。

我的項目結構是

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

我運行該測試是

@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() 

但是,我得到的錯誤

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 

「目錄名」 的使用方式是

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

這通常會返回

/home/jphamlett/Documents/Work/ServiceNowReportAutomation/ 

不過,我想這回在測試過程中

../working_sheets/ 

我甚至試圖用同樣的方法,日期時間

import os 


class MockOSPath(os.path): 

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

但是這給我的錯誤

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) 

我在做什麼錯?嘲笑的日期時間工作得很好。

回答

0

作爲測試的錯誤輸出表明您傳遞兩個參數測試:

  • self:測試類
  • @patch(「os.path.dirname」):修補對象

要修復它只是增加一個參數的方法簽名和實際測試使用它:

@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()