Mastermind - set up the Python skeleton

szabgab

Gabor Szabo

Posted on December 13, 2022

Mastermind - set up the Python skeleton

Git and README

The first thing was to create a git repository, write a README.md file describing the game. Similar to what I posted in the first article of this series.

Create the first test

Then create a file called tests/test_game.py with the following content:

import mastermind.game as mm

def test_game():
    mm.play()
    assert True
Enter fullscreen mode Exit fullscreen mode

It is not much, but I wanted to see that the tests are going to work.

I also installed pytest:

pip install pytest
Enter fullscreen mode Exit fullscreen mode

Then running pytest:

pytest
Enter fullscreen mode Exit fullscreen mode

got the following error

ModuleNotFoundError: No module named 'mastermind'
Enter fullscreen mode Exit fullscreen mode

This is of course not surprising as I have not created that module.

Creating the skeleton of the module

Next step was to create the file that will hold the implementation of the game. I created the file mastermind/game.py with the following content:

def play():
    ...
Enter fullscreen mode Exit fullscreen mode

Very minimalistic, I know, but enough to satisfy the test:

PYTHONPATH=. pytest
Enter fullscreen mode Exit fullscreen mode

This time it was successful.

Setting up GitHub Actions for Continuous Integration (CI)

Having tests is great. Having a CI to run them on every push and every pull-request is way better.

So I created the file .github/workflows/ci.yml with the following content:

name: CI

on:
  push:
  pull_request:
  workflow_dispatch:
#  schedule:
#    - cron: '42 5 * * *'

jobs:
  test:
    strategy:
      fail-fast: false
      matrix:
        runner: [ubuntu-latest, macos-latest, windows-latest]
        python-version: ["3.9", "3.10", "3.11"]

    runs-on: ${{matrix.runner}}
    name: OS ${{matrix.runner}} Python ${{matrix.python-version}}

    steps:
    - name: Checkout
      uses: actions/checkout@v3

    - name: Set up Python ${{ matrix.python-version }}
      uses: actions/setup-python@v4
      with:
        python-version: ${{ matrix.python-version }}

    - name: Install dependencies
      run: |
        # pip install -r requirements.txt
        pip install pytest

    - name: Check Python version
      run: python -V

    - name: Test with pytest
      env:
        PYTHONPATH: .
      run: pytest -vs
Enter fullscreen mode Exit fullscreen mode

It is funny that so far this is the most complex part of the project, but it won't change a lot.

The last file I added was the .gitignore file with the following content:

__pycache__
*.bak
Enter fullscreen mode Exit fullscreen mode

This will help me ensure that we don't add generated files to git by mistake.

Directory layout

To make it easier to understand the project I am also including the directory layout we have now:

.
├── mastermind
│   └── game.py
├── README.md
├── .gitignore
└── tests
    └── test_game.py
Enter fullscreen mode Exit fullscreen mode

Conclusion

OK, so when I started to write this project I thought I'll have more free time to make more progress, but after I published the introduction something came up and I only managed to get to this point. Nevertheless one have to start somewhere and it isn't that horrible to make slow progress.
I hope tomorrow I'll have some more time to work on this.

💖 💪 🙅 🚩
szabgab
Gabor Szabo

Posted on December 13, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related