单元测试不起作用,因为导入的文件作为输入
unittesting doesn't work because an imported file as inputs
我有一个实际结构的项目:
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:\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 不起作用的原因。我会尝试解决并返回答案。
问题已解决。
简单而漂亮,python 中有一行代码告诉 运行 仅当它直接启动时。
if __name__ == "__main__"
我认为该文件作为固有 __name__
是在启动时给出的,并且此名称仅在直接启动时才为 "__main__"
。因此,我可以导入设置,而不会遇到它在我面前启动并弄乱我的代码和测试的麻烦。
美好的一天!
我有一个实际结构的项目:
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:\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 不起作用的原因。我会尝试解决并返回答案。
问题已解决。
简单而漂亮,python 中有一行代码告诉 运行 仅当它直接启动时。
if __name__ == "__main__"
我认为该文件作为固有 __name__
是在启动时给出的,并且此名称仅在直接启动时才为 "__main__"
。因此,我可以导入设置,而不会遇到它在我面前启动并弄乱我的代码和测试的麻烦。
美好的一天!