Dec-17-2022, 08:38 AM
(This post was last modified: Dec-17-2022, 10:28 AM by Yoriz.
Edit Reason: Added code tags
)
hi,
I have small interactive calculator.
could you please give me a hint how could test it using pytest?
I found e.g. this approach, but have no idea how apply it:
https://stackoverflow.com/questions/5347...simulation
or maybe you have another idea.
thank you for any help!
I have small interactive calculator.
could you please give me a hint how could test it using pytest?
I found e.g. this approach, but have no idea how apply it:
https://stackoverflow.com/questions/5347...simulation
or maybe you have another idea.
thank you for any help!
#!/usr/bin/env python3 class Calculator: def __init__(self): pass def add(self, a, b): return a + b def sub(self, a, b): return a - b
#!/usr/bin/env python3 import calculator import cmd class InteractiveCalculator(cmd.Cmd): prompt = '(icalc) ' cal = calculator.Calculator() def do_add(self, arg): """A + B""" print(self.cal.add(*parse(arg))) def do_sub(self, arg): """A - B""" print(self.cal.sub(*parse(arg))) def do_exit(self, arg): """quits from the program""" exit() def parse(arg): """Convert a series of zero or more numbers to an argument tuple""" return tuple(map(int, arg.split())) if __name__ == '__main__': InteractiveCalculator().cmdloop()