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

2024-09-29 23:21:00 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

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

^{pr2}$

问题是,当我在testExample.py像这样

from setup import *

我启动单元测试,收到这个错误消息

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的输入的函数来替换input,但是仍然不起作用。我听说过模仿输入,但我不认为我应该这样做,我不是在尝试测试输入,我只想导入一个在脚本中使用输入的文件。。。在

如果有人知道如何解决这个问题,请帮忙!在

谢谢你!在

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

好像我进口的时候设置.py,它启动它!我试着在一个基本的程序中运行。。。这可能就是为什么unitest不起作用。我会设法解决,然后再给你一个答复。在


Tags: ofthenameinfrompytestinput
1条回答
网友
1楼 · 发布于 2024-09-29 23:21:00

问题已解决。在

简单而漂亮的是,python中有一行命令代码只有在直接启动时才运行。在

if __name__ == "__main__"

我相信这个文件是一个固有的__name__,它是在启动时给出的,这个名称只有在直接启动时才是"__main__"。因此,我可以导入安装程序,而不必面对启动它的麻烦,也不会弄乱我的代码和测试。在

再见!在

相关问题 更多 >

    热门问题