Reputation: 11
I have been working with python (in academia) for 5 years now but I had not written a test until last week. Two weeks ago I applied for a job and the employer asked me to do a python task and part of it should be unit testing. Assume that my code is as simple as follows (let call it "add_numbers.py"):
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('x', type=float)
parser.add_argument('y', type=float)
class add_numbers ():
def __init__ (self, x, y):
self.x = x
self.y = y
def add(self):
return self.x+self.y
if __name__ == '__main__':
args = parser.parse_args()
an = add_numbers(args.x,args.y)
print('{} + {} = {}'.format(args.x,args.y,an.add()))
and also assume that the test script is as follows and we name it "test_add_numbers.py":
import unittest
from add_numbers import add_numbers
class TddAdd(unittest.TestCase):
def test_add_two_numbers(self):
self.assertEqual(3, add_numbers(1,2).add())
if __name__ == '__main__':
unittest.main()
And then as instructions for running the code I wrote to him that you can run the test as
python test_add_numbers.py
and for running the main program you should type
python add_numbers 2 3
However he came back to me after 2-3 days and after saying that he regrets to reject me told me that I should have "automated" the test. What did he mean? So should I integrate the test in the main file such that when I run
python add_numbers 2 3
it first "automatically" runs the test and then run the main program itself.
What "automated" test means here and how to make the above test "automated"?
Upvotes: 1
Views: 334
Reputation: 446
That seems like a strange distinction to make. I would ask for clarification from the interviewer. It won't change their hiring decision, but it is completely reasonable for you to ask.
To me, an automated test would be something like running nosetests, but that isn't so different from unittest, just with a little more functionality. Oddly, most (or many) test packages in python are extensions of unittest.
Upvotes: 1