从python运行另一个python程序

问题描述 投票:0回答:1

我正在尝试构建测试脚本来测试我的python代码。

经过测试的代码应从stdin读取并写入stdout。

测试人员代码应调用经过测试的代码,将文件中的值作为stdin传递,并读取stdout以与预期值进行比较(存储在另一个文件中)

经测试的代码:

n = int(input())
x1,y1 = list(map(int, input().rstrip().split())) 
x2,y2 = list(map(int, input().rstrip().split())) 

#what it does is really not importante
if ((min(x1, x2) <= n/2) and (max(x1, x2) > n/2) or
    (min(y1, y2) <= n/2) and (max(y1, y2) > n/2)):
    print('S')
else :
    print ('N')

测试人员代码:

import os
import subprocess as sp
inputs = os.listdir("./warmup_tests/warmup_B/input")
for ipt in inputs:
    with open('./warmup_tests/warmup_B/input/{}'.format(ipt)) as f:
        res = (sp.run(['python', 'b.py'], stdin=f, capture_output=True))

我收到以下多个错误(在禁用capture_output以获得更好的可视化时:)

Traceback (most recent call last):
  File "b.py", line 10, in <module>
    x1,y1 = list(map(int, input().rstrip().split())) 
  File "<string>", line 1
    5 2
      ^
SyntaxError: unexpected EOF while parsing

我的输入文件如下:

10
5 2
5 1

当被测代码上只有一个input()时,以上方法起作用。使它与多个input()一起使用时我缺少什么?

python subprocess
1个回答
0
投票

您的代码对我有用(有很小的变化)。它可以和capture_output=True一起使用,也可以与capture_output=False

一起使用

https://drive.google.com/open?id=16NK1_Qfdd98prpmBUqaP894j8uGTNdnf

运行命令:

python3 tester.py

针对您的问题:我怀疑您的输入文件中某些行可能缺少'\ n'(可能是编辑器错误,不知道)。

也请尝试检查模块b.py中的缩进量>

您可以与输入文件共享代码以进行详细分析吗?

© www.soinside.com 2019 - 2024. All rights reserved.