Skip to content
This repository was archived by the owner on Nov 10, 2020. It is now read-only.

[Tester] Add runWithInput method to test_utils #119

@ebertmi

Description

@ebertmi

This is a convienence method for running a programming and capturing the in and output.

    def runWithInput(self, testInput=[]):
        studentSubmission = 'main'

        ## Stores output from print() in capturedOutput
        with patch('sys.stdout', new=StringIO()) as self.capturedOutput:
            #["4", "5", "1.2e-2", "2.5e2"]
            with patch('builtins.input', side_effect=testInput):
                # alternative kann mit side_effect=['Bart', 'Lisa'] mehr als ein Wert zurückgegeben werden
                if studentSubmission in sys.modules:
                    importlib.reload(sys.modules[ studentSubmission ] )
                else:
                    importlib.import_module( studentSubmission )

                # Now split the output into lines
                self.captured_stdout = self.capturedOutput.getvalue()
                self.lines = self.captured_stdout.split('\n')

                if len(self.lines) == 0:
                    self.fail('Es wurde nichts ausgegeben.')

                # Remove the last emtpy line
                if len(self.lines) > 0 and self.lines[-1] == '':
                    self.lines.pop()

        return self.lines

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions