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. Tox is a Python automation tool.
0:08 I have seen it most commonly used by developers who create Python packages.
0:12 That's because they need to test if they're package works with different versions of Python.
0:18 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 That's seven versions of Python.
0:28 If you want to manually tested, it will take you ages. You will need to create seven different virtual environments,
0:35 one for each different version of Python, then go inside each of them and run test. But you can instead automate all of that with Tox.
0:46 You will first have to write a configuration file called tox.ini and then in
0:51 that file, you specify one version of Python you want to use and what commands you want to run. The most common one is to run py test to make
1:00 sure that all tests are passing under each version of Python. But you can also run any additional command.
1:06 For example, you can run some static analysis tools like bandit to make sure that none of the Python versions has any security vulnerabilities.
1:16 Once you write this file, you just run Tox, command, and you check if there any errors, here We have a very simple example,
1:23 but Tox is actually quite powerful. One of the really cool feature is that you can specify a test matrix.
1:31 So you can say that you want to test different versions of Python, but also for each version of Python,
1:36 you want to use different version of a specific dependency. For example, here we say that we want to use Python 3.6,7 and 8,
1:45 and then for each of those Python versions, we want to use Django 2.2 and Django 3. And then for each of those Django versions,
1:54 we want to use SQlite and mySQL. As a database engine. This one line of configuration creates 12 different testing environments.
2:03 The first one will have Python 3.6, Django 2.2 and SQLite. The second one will have Python 3.6 and Django 2.2
2:12 as well, But this time we will use mySQL and so on Once we have all that prepared Tox will run tests inside each of them, trying
2:22 to re create and maintain This kind of set up by hand would take you a lot of time. And to be honest, it would be boring as hell.


Talk Python's Mastodon Michael Kennedy's Mastodon