archipylago

Your friendly Python community in Turku

archipylago = archipelago + python

Instructions for sprint, Feb 2024

Published by Juhis

Welcome to our first sprint! If you're not sure what to do or how to get started, we wrote this friendly guide. Don't hesitate to ask for help and pair up with others.

Choose one of the below.

I want to work on my own thing, alone

Find a seat and start working!


Practicing writing tests with Bulldoggy Reminder app

Bulldoggy Reminder app is an example app built with modern Python tooling (like FastAPI, htmx, Jinja, TinyDB, Playwright and pytest). It's a great starting point for practicing writing unit, API and UI/e2e tests.

The author, Andrew Knight gave a great talk about it in DjangoCon US 2023 if you want to learn more.

Setup

  1. Head over to AutomationPanda/bulldoggy-reminders-app and clone the project to your local developer environment:

    git clone git@github.com:AutomationPanda/bulldoggy-reminders-app.git
    cd bulldoggy-reminders-app
    
  2. Create a virtual environment and install the dependencies

    python -m venv venv
    source venv/bin/activate
    pip install -r requirements.txt
    playwright install # Optional if you're not going to write end-to-end or API tests
    
  3. Run the app locally

    uvicorn app.main:app --reload
    

If everything went fine, you should be able to check the app at http://localhost:8000 and see:

Bulldoggy front page with a login form

You can log in with credentials pythonista/I<3testing (see docs).

If not, check the repository for potential extra instructions.

How to run tests

There are three types of tests setup for you. To run all of them, make sure your app is running at localhost:8000 and then on a new terminal window, run

source venv/bin/activate
python -m pytest

To run only some tests, append the path of the file you want to test:

python -m pytest tests/test_unit.py # for unit tests

Write tests!

You can now spend the day practicing writing unit, API and e2e tests by writing functions that start with test_ into the test_*.py files in tests/ folder and the project will take care of running them. You can start by looking at the examples and coming up with your own tests. Don't hesitate to add or change features if you want to add new things for yourself to test.

The demo app uses Playwright for UI and API docs. Their documentation is quite nice.

Ideas for what to test

  • Logging in (success and fail cases)
  • Creating a new list
  • Creating a new reminder
  • Logging out and seeing if they still exist
  • Editing lists and reminders
  • Deleting lists and reminders
  • Checking that users cannot see other users' reminders
  • Testing with different types of inputs (emojis! non-ASCII characters! different encodings! XSS!)
  • Use coverage.py to see what code is covered by tests or not
  • Look up and use pytest's parametrize for multiple test inputs

Troubleshooting

  • If you see an error like FAILED tests/test_api.py::test_successful_api_login - playwright._impl._errors.Error: connect ECONNREFUSED 127.0.0.1:8000, that's because the app isn't running.

  • If you see an error like

    ImportError while loading conftest '/bulldoggy-reminders-app/tests/conftest.py'.
    tests/conftest.py:19: in <module>
        from testlib.inputs import User
    E   ModuleNotFoundError: No module named 'testlib'
    

    it's likely you ran pytest instead of python -m pytest

  • If you seen an error like

    ImportError while loading conftest '/bulldoggy-reminders-app/tests/conftest.py'.
    tests/conftest.py:18: in <module>
        from playwright.sync_api import Playwright
    E   ModuleNotFoundError: No module named 'playwright'
    

    it's likely you didn't activate the virtualenv again (with source venv/bin/activate) after opening a new tab.

  • If you see an error like

    E       playwright._impl._errors.Error: Executable doesn't exist at /Library/Caches/ms-playwright/chromium-1097/chrome-mac/Chromium.app/Contents/MacOS/Chromium
    

    run playwright install to install browsers for e2e tests.


Practicing pair-programming and test-driven development through a kata

Raniz Daniel Raneland has created a great multi-step kata to use for TDD practicing. It's called CEO Bowling and with it, you'll start with a problem description and need to write a solution to it before you're able to move to the next step.

How to TDD?

Test-driven development (TDD) is a popular way to write tests. In it, you write a test first and then implement a minimal solution that makes that test pass.

The flow is called Red-Green-Refactor and it goes like this:

  1. Write a test and make sure it fails
  2. Write code that makes the test pass
  3. Refactor the code
  4. Go to step 1 and write a new test

The idea is to build the application in small steps that are all tested.

Raniz has written a bit more about this kata and TDD in his blog.

How to pair?

  1. Find a friend
  2. Either choose to work on the same laptop or bring your own laptops and install mob.sh
  3. Choose a method:
    3.1 One person writes a test, another writes code that makes it pass. Switch for the next test
    3.2 For a full cycle, one person is the driver (writes code) and the other is a navigator (tells what to write).

Often it's recommended to use one computer and pass the keyboard along but I've found that often it's hard to write on someone else's computer, especially if people are used to different operating systems or keyboard layouts (I use US/Mac layout and once paired with someone who used a Czech/PC layout and it was a mess).

If you want to use two computers, install mob.sh and create a repository somewhere online (for example, GitHub or Bitbucket). Then both clone that repository.

To use mob, here are the command line commands:

mob start # pulls the latest changes to your computer
mob next # commits and pushes the changes

Whenever you switch drivers (see #3 above), run mob next on the first computer and then mob start on the other. This will sync your code across the two computers as you work on the code.

When you're done (for example, finishing a section of the kata), you can run

mob done
git commit
git push

to properly store your changes in the repository.

At the end of the exercise, you'll both have all the code and changes in your laptop!

Contributions

Thanks to Hugo for contributing with fixes and ideas!