doctest и unittest с помощью pysandbox
Я хочу проверить студенческие работы в среде сохранения. Вот почему я использую pysandbox. Для тестирования студенческих работ я хочу использовать doctest и unittest.
Вот этот файл studentSubmission.py
def factorial(n):
import math
if not n >= 0:
raise ValueError("n must be >= 0")
if math.floor(n) != n:
raise ValueError("n must be exact integer")
if n+1 == n: # catch a value like 1e300
raise OverflowError("n too large")
resulto = 1
factor = 2
while factor <= n:
resulto *= factor
factor += 1
return resulto
Вот регулярный юнит-тест от возможного наставника: tutor_tester.py
import unittest
from studentSubmission import factorial
class TestSequenceFunctions(unittest.TestCase):
def test_equal(self):
res = factorial(4)
self.assertEqual(res, 24)
def test_error(self):
self.assertRaises(ValueError, factorial, -1)
if __name__ == '__main__':
unittest.main()
А теперь скрипт для песочницы: sandbox_test.py
import os
from sandbox import Sandbox, SandboxConfig
#sandbox config
sandbox = Sandbox(SandboxConfig('traceback','math','stdout','stderr','exit' ))
sandbox.config.allowModule('unittest','student_submission')
fo = open("./tutor_tester.py", "r")
code = fo.read();
fo.close()
sandbox.execute(code)
Когда я запускаю это на моем Ubuntu LTS 12.04.3 с версией Python 2.7.3 и последней версией Pysandbox. Запустив sandbox_test.py, я получил следующую ошибку:
Traceback (most recent call last):
File "sandbox_test.py", line 17, in <module>
sandbox.execute(code)
File "/usr/local/lib/python2.7/dist-packages/sandbox/sandbox_class.py", line 97, in execute
return self.execute_subprocess(self, code, globals, locals)
File "/usr/local/lib/python2.7/dist-packages/sandbox/subprocess_parent.py", line 185, in execute_subprocess
raise output_data['error']
ImportError: Import "result" blocked by the sandbox
Когда я пробую doctest: doctest_sandbox.py
def testing():
"""testing student
>>> from studentSubmission import factorial
>>> [factorial(n) for n in range(6)]
[1, 1, 2, 6, 24, 120]
>>> [factorial(long(n)) for n in range(6)]
[1, 1, 2, 6, 24, 120]
>>> factorial(30)
265252859812191058636308480000000L
>>> factorial(-1)
Traceback (most recent call last):
...
ValueError: n must be >= 0
"""
if __name__ == "__main__":
import doctest
from sandbox import Sandbox, SandboxConfig
sandbox = Sandbox(SandboxConfig('math','stdout','stderr','exit' ))
sandbox.config.allowModule('studentSubmission')
sandbox.execute(doctest.testmod())
Doctest работал довольно хорошо, но в итоге песочница выдала мне ошибку:
python doctest_sandbox.py -v
Trying:
from studentSubmission import factorial
Expecting nothing
ok
Trying:
[factorial(n) for n in range(6)]
Expecting:
[1, 1, 2, 6, 24, 120]
ok
Trying:
[factorial(long(n)) for n in range(6)]
Expecting:
[1, 1, 2, 6, 24, 120]
ok
Trying:
factorial(30)
Expecting:
265252859812191058636308480000000L
ok
Trying:
factorial(-1)
Expecting:
Traceback (most recent call last):
...
ValueError: n must be >= 0
ok
1 items had no tests:
__main__
1 items passed all tests:
5 tests in __main__.testing
5 tests in 2 items.
5 passed and 0 failed.
Test passed.
Traceback (most recent call last):
File "doctest_sandbox.py", line 21, in <module>
sandbox.execute(doctest.testmod())
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sandbox/sandbox_class.py", line 97, in execute
return self.execute_subprocess(self, code, globals, locals)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sandbox/subprocess_parent.py", line 185, in execute_subprocess
raise output_data['error']
TypeError: exec: arg 1 must be a string, file, or code object
Спасибо за вашу помощь;)