Modern Python Projects Transcripts
Chapter: CI
Lecture: tox - automate Python tasks on your computer

Login or purchase this course to watch this video and the rest of the course contents.
0:00 One way to automate those tasks is to use a tool like Tox.
0:04 Tox is a python automation tool.
0:07 I have seen it most commonly used by developers who create python packages.
0:11 That's because they need to test if they're package works with different versions of python.
0:17 Let's say you need to make sure that your Python package works with python 3.4 3.6, 6,7,8,9 and 10
0:22 That's seven versions of python.
0:27 If you want to manually tested,
0:29 it will take you ages. You will need to create seven different virtual environments,
0:34 one for each different version of python,
0:37 then go inside each of them and run test.
0:40 But you can instead automate all of that with Tox.
0:45 You will first have to write a configuration file called tox.ini and then in
0:50 that file, you specify one version of python you want to use and what commands
0:55 you want to run. The most common one is to run py test to make
0:59 sure that all tests are passing under each version of python.
1:03 But you can also run any additional command.
1:05 For example, you can run some static analysis tools like bandit to make sure that
1:10 none of the python versions has any security vulnerabilities.
1:15 Once you write this file, you just run Tox,
1:18 command, and you check if there any errors, here
1:21 We have a very simple example,
1:22 but Tox is actually quite powerful.
1:25 One of the really cool feature is that you can specify a test matrix.
1:30 So you can say that you want to test different versions of python,
1:33 but also for each version of python,
1:35 you want to use different version of a specific dependency.
1:38 For example, here we say that we want to use Python 3.6,7 and 8,
1:44 and then for each of those Python versions,
1:46 we want to use Django 2.2 and Django 3.
1:50 And then for each of those Django versions,
1:53 we want to use SQlite and mySQL.
1:55 As a database engine. This one line of configuration creates 12 different testing environments.
2:02 The first one will have python 3.6,
2:05 Django 2.2 and SQLite. The second one will have python 3.6 and Django 2.2
2:11 as well, But this time we will use mySQL and so on
2:15 Once we have all that prepared Tox will run tests inside each of them, trying
2:21 to re create and maintain This kind of set up by hand would take you a
2:25 lot of time. And to be honest,
2:27 it would be boring as hell.