📜 ⬆️ ⬇️

Python. Unit Test Generation

Only the lazy one has not yet written about the need to write tests. But let's be honest - writing tests is often boring. Especially for legacy code. Hundreds of duplicate, monotonous lines. Boredom. What can be done with this?

image
Picture to attract attention. Beautiful python, yes? (Author of the photo: Paweł Stefaniak)

Pythoscope


Fortunately, we are programmers - people are lazy, ready to kill a couple of weeks in order to solve an hourly task in five minutes. So in a few minutes of googling in Yandex, I found colleagues in misfortune and an interesting solution to facilitate the writing of tests.

Install Pythoscope:
')
sudo pip install Pythoscope 


Note
Unfortunately, the stable branch has problems with unicode. For test purposes, it will come down, but for real use it is better to use the dev branch, since it works stably:

 sudo apt-get install bzr bzr branch lp:pythoscope cd pythoscope/ python setup.py install 




Test cat


image

To test a cat, we first actually need a cat. We write it:
 # cat.py class Cat(object): def __init__(self, name='Tom'): self.name = name def eat(self, food): if food == 'fish': return 'Yummy!' else: return 'Ugh!' 


Now go to the folder with cat.py and initialize Pythoscope:

 pythoscope --init 


The command will create a .pythoscope folder where all information related to Pythoscope is stored. And now, finally, the generation of the actual tests:

 pythoscope cat.py 


We have the tests folder with the cat_test.py attached. In which ... There is almost nothing:

 # tests/cat_test.py import unittest class TestCat(unittest.TestCase): def test___init__(self): # cat = Cat(name) assert False # TODO: implement your test here def test_eat(self): # cat = Cat(name) # self.assertEqual(expected, cat.eat(food)) assert False # TODO: implement your test here if __name__ == '__main__': unittest.main() 


Not much? Well, at least now there is a framework that will save us time when writing tests.
As it turned out, there is no magic - the tests themselves will not be written. But all is not lost, we can help Pythoscope understand what to run for tests. For this purpose, the so-called “entry points” (points of entry) are real — some use-case uses of our code.

Let's write the points of entry for our cat:

 # .pythoscope/points-of-entry/eat_fish_poe.py from cat import Cat Cat().eat('fish') 


Run the test generation again:
 pythoscope cat.py 


Now, better, a method has been added to the test class that is actually testing something:
 # tests/cat_test.py ... def test_eat_returns_Yummy_for_fish_after_creation_with_Tom(self): cat = Cat('Tom') self.assertEqual('Yummy!', cat.eat('fish')) ... 


And what if you slip a kote not a fish? Mistake? It is necessary to check this:
 # .pythoscope/points-of-entry/eat_tomato_poe.py from cat import Cat Cat().eat('tomato') 


We generate:

 pythoscope cat.py 


Great, it remains only to check:

 nosetests .. ---------------------------------------------------------------------- Ran 2 tests in 0.001s OK 


Excellent! Successful and easy testing!

Source: https://habr.com/ru/post/192512/


All Articles