Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Testing Your Code on Push

00:00 In this lesson, you’ll create a testing workflow, and this workflow should run on every push to the main branch. To save yourself some work, you can duplicate the hello_python.yaml workflow and name it test.yaml.

00:15 Make sure that the file is in the .github/ workflows/ folder. As a name, you can also use test.

00:23 And as for the trigger, let’s be a bit more specific this time. So far, this test workflow would run on any push to any branch, but you can also be more exact and say, I don’t care about any other branches, I only want to run this workflow when the push is to the main branch.

00:41 So remove the push that you’re currently having and then on the next line, indented push:, and on the next line, again indented once more branches:, and then on a next line indented once more - main, so that way you are telling GitHub to only run this workflow when there is a push on the main branch.

01:05 Inside the jobs section, you can remove the run_python section where you check the Python version that Ubuntu has, and then you can rename the run_specific_python job to test. As a friendly name, you can write Run Tests With Python 3.13.

01:24 Now, since you want to interact with files in your repo, you must make use of the checkout action that was mentioned before in this course.

01:32 Commonly you add this action as one of the first steps. So similarly to the setup-python action, you use the uses property with a dash in front.

01:44 So - uses: actions/checkout @v4.

01:51 Then you keep the part that sets up Python, and it also doesn’t hurt to keep the run python --version command there. But now comes the more important part.

02:04 Have a new line with - run: python -m pip install -r requirements-dev.txt. So with a new line where you have the run property, you can think of it like having a new line in your terminal.

02:21 So after installing the dev dependency, write a new line - run:, and then you want to run pytest, and that’s it. So to see if the workflow works as expected, commit and push your changes,

02:38 and then hop over to the browser.

02:43 Pushing your changes triggers two workflows: the test workflow, and the former hello_python workflow. If you click on the test workflow, on the run test with Python 3.13 and there on the run Python step, you can verify that your test passed.

03:02 As you can imagine, you could run a whole test suite that always runs and needs to pass before, for example, you allow merging a pull request. And if this sounded a bit complicated to you, I will have a resource in the summary lesson where you will find a tutorial that goes deeper into the ways how you can use GitHub Actions. In this course, you basically scratched the surface of what GitHub Actions can do, and if you come up with some eloquent GitHub Actions and workflows, feel free to share them in the comments below this video.

03:38 In our next lesson, we’ll wrap up this course and I will give you some additional resources that you can watch and read after this course.

Become a Member to join the conversation.