假设我想用测试给学生的python代码评分,比如(这是我希望我可以写的伪代码):code = __import__("student_code") # Import the code to be tested
grade = 100
for test in all_tests(): # Loop over the tests that were gathered
good = perform(test, code) # Perform the test individually on the code
if not good: # Do something if the code gives the wrong result
grade -= 1
为此,我想使用pytest(易于编写好的测试),但有许多事情我不知道如何做:如何在外部代码上运行测试?(这里是从学生代码导入的code)
如何列出所有可用的测试?(此处all_tests())
如何在代码上单独运行它们?(此处perform(test, code))
我找不到与此用户案例相关的任何内容(pytest.main()似乎无论如何都不起作用…)
希望你明白我的意思,干杯!在
编辑
我终于找到了如何执行我的第一点(对外部代码进行测试)。在要执行测试的存储库中,生成一个conftest.py文件:
^{pr2}$
然后,使用module夹具将测试收集到tests.py文件中:def test_sample(module):
assert module.add(1, 2) == 3
最后,使用py.test tests.py --module student.py运行测试。在
我还在研究第2点和第3点。在
编辑2
我上传了我对这个问题的看法(不完整):
欢迎帮助和贡献!在