【问题标题】:Testing student scripts in Python 3在 Python 3 中测试学生脚本
【发布时间】:2016-04-22 19:58:28
【问题描述】:

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

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

知道紧缩... 我正在努力编写一个脚本,该脚本将遍历子文件夹系统以针对所有提交运行我的测试脚本。您可以想象,这将大大有助于减少标记时间。

文件夹结构是由他们以电子方式提交作品的方式生成的。我最终得到了一个作业文件夹,然后是学生工作的子文件夹。 例如“作业 1 文件夹”,然后在其中为每个提交的学生创建一个文件夹,例如“Anthony Student Folder”、“Another Student 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 个测试(模块)以在脚本上测试/运行。 所以我希望遍历学生文件夹列表并导入学生解决方案并使用测试用例运行。

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

【问题讨论】:

  • 你的问题很清楚,但到目前为止你做了什么?您在代码中遇到过哪些问题?

标签: python python-3.x automated-tests


【解决方案1】:

这就是这段代码的作用,它将把一个文件夹中的所有文件列表保存在一个列表中,对于该文件夹中的每个文件,您都可以使用它们的文件名作为参数来运行脚本。如果这似乎可以解决您的问题,您可以使用以下代码。

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

【讨论】:

    猜你喜欢
    • 2011-08-23
    • 1970-01-01
    • 2014-05-26
    • 1970-01-01
    • 2013-02-08
    • 2015-02-07
    • 2016-04-12
    • 1970-01-01
    • 2011-10-14
    相关资源
    最近更新 更多