Python 如何修复与Pytest一起工作的程序?手动运行时,它工作正常
我有一个关于pytest的问题。我有一个在终端上运行时运行良好的程序:Python 如何修复与Pytest一起工作的程序?手动运行时,它工作正常,python,pytest,Python,Pytest,我有一个关于pytest的问题。我有一个在终端上运行时运行良好的程序: "python ./solution/program1.py ./data/set.data 15 123 output-1.txt" 这里 program1.py - program name set.data - input data set 15 - input program parameter 123 - input program parameter output-1.
"python ./solution/program1.py ./data/set.data 15 123 output-1.txt"
这里
program1.py - program name
set.data - input data set
15 - input program parameter
123 - input program parameter
output-1.txt- output file
为程序生成输出文件
我有一个pytest来检查我的结果。这不适用于pytest。
当我运行pytest时,没有输出,pytest继续运行,没有任何结果。我必须手动停止它。我有什么办法可以修好它吗
Pytest:
import subprocess, os
def test_kmeans():
file_name = "output-1.txt"
command="python ./solution/program1.py ./data/set.data 15 123 {}".format(file_name)
process = subprocess.Popen(command, shell=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
code=process.wait()
assert(not code), "Command failed"
assert(open(file_name,"r").read()==open("tests/final-results.txt","r").read())
这是否是当前工作目录问题,其中
subprocess.Popen
在OS目录中而不是在您期望的目录中打开?问题是因为程序未被优化,在测试调用中指定了绝对路径而不是相对路径。