我是一名中学教师,几年来一直在使用Python解决简单的任务。我对编写在学生代码上运行的自动化测试以自动化评分过程很感兴趣。
我也对TFD很感兴趣,所以单元测试似乎是一种很自然的探索方式。我已经写了一个测试,我要求学生修改脚本中的几个函数和过程。然后,我可以对每个提交文件手动运行一个测试脚本,通过导入那里的文件来给它们一个分数。
知道问题的关键..。我正在努力编写一个脚本,它将遍历一个子文件夹系统,以便对所有提交的内容运行我的测试脚本。正如您可以想象的那样,这将对减少标记时间有很大帮助。
文件夹结构是由他们以电子方式提交工作的方式生成的。我最终得到了一个作业文件夹,然后是其中的一个子文件夹,里面有学生们的作业。例如:"Assignment 1 Folder“,然后在其中为每个提交的学生创建一个文件夹,例如“安东尼学生文件夹”,“另一个学生文件夹”等(大约23名学生)。
每个学生将编辑一个脚本,要求他们编写一个函数或过程。下面是一个示例:
# =======================================================================
# Test 1
# Write a function called 'MyCubed' that takes an integer number as an
# argument and returns the cube of that number. E.g. calling it with 2
# should give 8.
# =======================================================================
# Code HERE the following code is a student response.
def MyCubed(num):
return num**3所以我已经创建了一个测试文件,我可以将它放在每个学生文件夹中,然后运行它来测试每个文件。
# =======================================================================
# Test 1
# Cube an integer
test1 = 0
ModuleExist = True
try:
test1 = Python_Test.MyCubed(3)
except:
print('\nTest 1: Failed: MyCubed not present')
ModuleExist = False
if ModuleExist:
if test1 == 27:
print('\nTest 1: My Cubed Passed')
score += 10
elif test1 != 0:
print('\nTest 1: Failed expected 27, actually-', test1)此脚本包含要在脚本上测试/运行的8个测试(模块)。因此,我希望遍历学生文件夹列表,导入学生解决方案并运行测试用例。
我可以将文件放在每个文件夹中,然后单独运行它们,但我想让它自动遍历所有子文件夹。
发布于 2016-04-21 13:56:03
这是这段代码要做的,它将把文件夹中的所有文件列表保存在一个列表中,并且对于该文件夹中的每个文件,您可以使用其文件名作为参数来运行您的脚本。如果这看起来像是您问题的解决方案,您可以使用以下代码。
import os
from subprocess import Popen
content_list = []
for content in os.listdir(<Path>): # Path to the directory where student Scripts were there
content_list.append(content)
for item in content_list:
pobj = Popen([executable, '<Your_script>'+item], bufsize=-1, stdout=sout, stderr=serr)
pobj.wait()https://stackoverflow.com/questions/36760436
复制相似问题