Hello, Habrahabr! I want to give your attention my project for unit testing. More precisely - a tool to think about the tests better, instead of spending too much attention on the creation of files, declarations of imports, classes and test cases.
The article contains a description of the project, how to install and use it, and examples.
Introduction
utbone is a command line library (
link to a Github repository ) written in Python. It stands for “unit-testing bone”, which means “the backbone for unit testing”. Using a single command in the console, you can create a template for testing. This will save some of the time on routine things like setting up "setup" functions, testing under
django , for example. Another mini-feature is “asserses” in one of the tests, you can immediately run the tests and see if they are performed at all in this directory or not.
utbone mysite/polls/tests test_views django --topping
Using the command above in the console, you can create the following python file for tests called
test_views in the test folder, aimed at testing
django using the external
mock and
ddt libraries.
')
""" Tests for """ from ddt import ddt, data, unpack from mock import patch from django.test import TestCase class Test(TestCase): """ Tests for """ def setUp(self): """ Initialize """ pass def test_first(self): """ Verify that """ self.assertEqual(1, '1') @patch('') def test_second(self, mock_): """ Assert that """ pass @data( ('first', 'second', 'expected'), ('first', 'second', 'expected'), ) @unpack def test_third(self, first, second, expected): """ Make sure when """ pass
Basic patterns
The cores for testing differ in the type of tests and in the presence of toppings (all templates are available on Github by
reference ).
The types at this stage of development of the project include:
- unittest - testing through the unittest module built into Python;
- django is the backbone of testing for django .
The presence of toppings is expressed through the
--topping flag at the end of the template creation command in the console. It allows you to add frequently used
mock and
ddt programmers to imports and declare their main tools in the code - for example, the decorator
patch and
data on top of test methods.
... @patch('') def test_second(self, mock_): """ Assert that """ pass @data( ('first', 'second', 'expected'), ('first', 'second', 'expected'), ) @unpack def test_third(self, first, second, expected): """ Make sure when """ pass ...
How to use
At this stage, you can already start using the library. The surest way is to install via
pip .
pip install utbone
utbone takes three arguments: where to create the file, with what name and what kind.
You can add a fourth - the presence of toppings (via -t or --toppings).
utbone mysite/polls/tests test_views django --topping
Using the
--help flag, get instructions on how to use the tool locally.
utbone --help
Thank you for your attention.