unittesting不起作用,因为导入的文件作为输入

时间:2018-05-15 14:14:25

标签: python unit-testing input mocking directory-structure

我有一个项目作为实际结构:

Project
   |-setup.py
   |
   |-Code
   |  |-exemple.py
   |
   |-Test
      |-testExemple.py

我想让我的设置自动设置文件和路径目录,无论它们是什么。所以在我的Setup.py中,我有30个导入,但后来我有输入:

testFolderNameSetup = input("Enter the name of the folder containing your tests:")
testFolderNameSetup = "./" + str(testFolderNameSetup)
testFolderPathNameSetup = (os.path.abspath(os.path.join(os.path.dirname(__file__), testFolderNameSetup)))

if os.path.exists(testFolderPathNameSetup):
     sys.path.insert(0, testFolderPathNameSetup)

问题是,当我在testExample.py中导入设置时,就像这样

from setup import *

和la lauch单元测试,我收到此错误消息

Testing started at 9:02 AM ...
C:\Users\marc-\AppData\Local\Continuum\anaconda3\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.2\helpers\pycharm\_jb_pytest_runner.py" --target testExemple.py::TestExample.testAdd
Launching py.test with arguments testExemple.py::TestExample::testAdd in C:\Users\marc-\Documents\GitHub\BasicProjectArchitecture\test

============================= test session starts =============================
platform win32 -- Python 3.6.3, pytest-3.2.1, py-1.4.34, pluggy-0.4.0
rootdir: C:\Users\marc-\Documents\GitHub\BasicProjectArchitecture, inifile:Enter the name of the folder containing your code:
test/testExemple.py:None (test/testExemple.py)
testExemple.py:2: in <module>
    from setup import *
..\setup.py:21: in <module>
    codeFolderNameSetup = get_input("Enter the name of the folder containing your code:")
..\setup.py:14: in get_input
    return input(text)
..\..\..\..\AppData\Local\Continuum\anaconda3\lib\site-packages\_pytest\capture.py:459: in read
    raise IOError("reading from stdin while output is captured")
E   OSError: reading from stdin while output is captured

=================================== ERRORS ====================================
____________________ ERROR collecting test/testExemple.py _____________________
testExemple.py:2: in <module>
    from setup import *
..\setup.py:21: in <module>
    codeFolderNameSetup = get_input("Enter the name of the folder containing your code:")
..\setup.py:14: in get_input
    return input(text)
..\..\..\..\AppData\Local\Continuum\anaconda3\lib\site-packages\_pytest\capture.py:459: in read
    raise IOError("reading from stdin while output is captured")
E   OSError: reading from stdin while output is captured
------------------------------- Captured stdout -------------------------------
Enter the name of the folder containing your code:
=========================== 1 error in 0.22 seconds ===========================
ERROR: not found: C:\Users\marc-\Documents\GitHub\BasicProjectArchitecture\test\testExemple.py::TestExample::testAdd
(no name 'C:\\Users\\marc-\\Documents\\GitHub\\BasicProjectArchitecture\\test\\testExemple.py::TestExample::testAdd' in any of [<Module 'test/testExemple.py'>])

Process finished with exit code 0

是的,错误是get_input,只是因为我尝试用返回名为get_input的输入的函数替换输入,但它仍然无法正常工作。我听说过嘲笑输入,但我不认为我应该这样做,我没有尝试测试输入,我只想导入一个在其脚本中使用输入的文件......

如果有人知道如何解决此问题,请提供帮助!

谢谢!

--------------------编辑#1 -----------------

似乎当我导入setup.py时,它会启动它!我尝试在基本功能中导入并运行设置代码......这可能就是为什么unitest不起作用。我会尝试解决这个问题并回答。

1 个答案:

答案 0 :(得分:0)

问题解决了。

简单而精美的是,python中的一行告诉代码只有在直接启动时才会运行。

with t (id,title,genre,languageids)
as (
select 1,      'Movie 1',        'Action',      '1,2,5,8,10' union all
select 2,      'Movie 2',        'Romance',     '2,4'        union all
select 3,      'Movie 3',        'Comedy',      '3,8,21'
)
select * from t where (',' + languageids + ',') like '%,2,%'

我认为该文件是在启动时提供的固有if __name__ == "__main__" ,并且只有在直接启动时才会显示此名称__name__。因此,我可以导入设置,而不必在我面前启动它并弄乱我的代码和测试。

美好的一天!