/PythonCodeExercises

Test and creating python samples to do Katas with and to keep. readme of good practises

Introduction

Create simple unit testing sample and understand good naming and generation of samples ...

The inspiration for this is taken from this article https://testdriven.io/blog/modern-tdd/

Setup

  1. Install pip install -U pytest

  2. pytest is the test framework used by the article

  3. Documentation can be found https://docs.pytest.org/en/stable/getting-started.html

  4. Running the tests through python I setup python3 -m pytest . NOTE: the test name always needs to start with test_ or end with _test or the test discovery will not happen

  5. VSCode Tips and tricks:

    • With vscode, I needed to setup my interpreter path to utilise Python 3.9.1 for everythign to work with the correct version of Python. This can be found on the bottom left panel of your IDE footer

    • Setting up text explorer, add the extension python test explorer and then see this https://code.visualstudio.com/docs/python/testing for more information

    • Configure settings with Pytest

      {
          "python.pythonPath": "/opt/local/bin/python3",
          "python.testing.pytestArgs": [
              "tests"
          ],
          "python.testing.unittestEnabled": false,
          "python.testing.nosetestsEnabled": false,
          "python.testing.pytestEnabled": true,
          "python.testing.autoTestDiscoverOnSaveEnabled": true
      }
  6. Install pip with python 3 or upgrade pip to the latest

    python3 -m pip install
    python3 -m pip install --upgrade pip
  7. Create a virtual environment using python3 -m venv ./opt/local

  8. Setting up a project structure should resemble https://docs.python-guide.org/writing/structure/

  9. Make sure if you have folder that you always include __init__.pyfiles which can include paths, setting variables or just logging - see more here

  10. Pytest install all things needed:

    pip install pytest
    pip install pytest-sugar
    pip install pytest-cov
    
    • pytest --fixtures gives a list of fixtures, including one built up with setup fixtures that may have doc comments
    • pytest --markers gives a list of markers to help decorate functions with
    • pip install pytest-html and output html to show reports and output an html report pytest --html=report.html
    • Mocking details can be here within https://docs.python.org/3/library/unittest.mock.html#module-unittest.mock
    • pytest --cov-report html:cov_html --cov-branch --cov=<add specific module> . for getting coverage of a specific module being tested
    • pytest --cov=myproj tests/ should do a report for everything but it doesnt work for me at the moment