单元测试不起作用,因为导入的文件作为输入



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

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 *

我进行单元测试,我收到此错误消息

Testing started at 9:02 AM ...
C:Usersmarc-AppDataLocalContinuumanaconda3python.exe "C:Program FilesJetBrainsPyCharm Community Edition 2018.1.2helperspycharm_jb_pytest_runner.py" --target testExemple.py::TestExample.testAdd
Launching py.test with arguments testExemple.py::TestExample::testAdd in C:Usersmarc-DocumentsGitHubBasicProjectArchitecturetest
============================= test session starts =============================
platform win32 -- Python 3.6.3, pytest-3.2.1, py-1.4.34, pluggy-0.4.0
rootdir: C:Usersmarc-DocumentsGitHubBasicProjectArchitecture, 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)
........AppDataLocalContinuumanaconda3libsite-packages_pytestcapture.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)
........AppDataLocalContinuumanaconda3libsite-packages_pytestcapture.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:Usersmarc-DocumentsGitHubBasicProjectArchitecturetesttestExemple.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 不起作用的原因。我会尝试解决它并返回答案。

问题已解决。

简单而漂亮地,python中有一行告诉代码只有在直接启动时才运行。

if __name__ == "__main__"

我相信该文件是启动时给出的固有__name__,并且只有在直接启动时才会"__main__"此名称。因此,我可以导入设置,而不会遇到麻烦,让它在我的脸上启动并弄乱我的代码和测试。

日安!

最新更新