1

I have recently been tasked with teaching primary school kids python 3, I am okay at it but am still learning. I thought the easiest thing for me to do would be use repl.it classroom edition to allow for the students to access it at home if they wanted. Included in the repl.it classroom is a auto marking system where you can use unittest to check if their code is correct. I have worked out how to check that variables are correct but I am having difficulty checking the output. The code they are writing is as followed

a = 5
b = 10

print( a + b )

What I need to do is check that they are printing 15. I have tried the following so far, I have imported sys and the unittest function has already been created by repl.it so it's just this function that I can manipulate.

def test_output(self):
    output = sys.stdout
    self.assertEqual( output, 25 )

but i know this is not correct. If anyone could help me find how to check the output that would be great.

Morgan Thrapp
  • 9,748
  • 3
  • 46
  • 67
TheSk8rJesus
  • 418
  • 2
  • 12
  • 1
    Related: http://stackoverflow.com/questions/4219717/how-to-assert-output-with-nosetest-unittest-in-python – Scott Mermelstein Dec 09 '16 at 16:48
  • Why re-invent the wheel ? Your implementation is going to be worse that projects built by large team of developers. Maybe https://www.codecademy.com/ or some kid-specific resource ? – Giannis Dec 09 '16 at 17:00

1 Answers1

0

I would try to mock or patch the builtin print function. Then assert on the mock that it was called with the right parameters. This is maybe impossible with repl.it though. Can you access some setup/teardown mechanism for your test?

Patching would look like this:

@mock.patch('builtins.print')
def test_some(self, mock_print)
    ...would expect call to pupil code here
    mock_print.assert_called_once_with (... your expectation)
Markus W.
  • 121
  • 4