2016-04-21 38 views
0

我是中学的老师,几年来一直在使用Python解决简单的任务。我很有兴趣编写自动化测试来运行学生代码以使标记过程自动化。在Python 3中测试学生脚本

我也对TFD感兴趣,所以单元测试似乎是一种自然的探索途径。我写了一个测试,要求学生修改脚本中的几个函数和过程。然后我可以手动运行一个测试脚本对每个提交给他们通过导入文件的分数。

知道紧缩... 我很努力地编写一个脚本,它将遍历子文件夹系统来针对所有提交运行我的测试脚本。正如你可以想象的那样,这对于减少打标时间会有重要的帮助。

文件夹结构是通过电子方式提交作品的方式生成的。我最终得到了一个分配文件夹,然后在这里与学生一起工作的子文件夹。 例如“作业1文件夹”,然后在每个学生提交的文件夹内“安东尼学生文件夹”,“另一个学生文件夹”等(约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个测试(模块)以在脚本上测试/运行。 所以我希望遍历学生文件夹的列表并导入学生解决方案并运行测试用例。

我可以将文件放在每个文件夹中并单独运行它们,但我想自动执行它以遍历所有子文件夹。

+0

你的问题很清楚,但你到目前为止做了什么?你的代码遇到了哪些问题? – EbraHim

回答

0

下面是代码将执行的操作,它将保存文件夹中的所有文件列表,并为该文件夹中的每个文件保存文件名作为参数。如果这看起来像解决您的问题,您可以使用下面的代码。

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()