makePythonTdd.sh

makePythonTdd.sh program

  • Here is the makePythonTdd.sh program from how to make a python test driven development environment

     1#!/bin/bash
     2PROJECT_NAME=$1
     3mkdir $PROJECT_NAME
     4cd $PROJECT_NAME
     5mkdir src
     6touch src/$PROJECT_NAME.py
     7mkdir tests
     8touch tests/__init__.py
     9
    10echo "import unittest
    11
    12
    13class Test$PROJECT_NAME(unittest.TestCase):
    14
    15    def test_failure(self):
    16        self.assertFalse(True)
    17
    18
    19# Exceptions seen
    20# AssertionError
    21" > tests/test_$PROJECT_NAME.py
    22
    23python3 -m venv .venv
    24source .venv/bin/activate
    25python3 -m pip install --upgrade pip
    26echo "pytest-watch" > requirements.txt
    27python3 -m pip install --requirement requirements.txt
    28pytest-watch
    
  • use chmod to make the program executable

    chmod +x makePythonTdd.sh
    
  • give a name for the PROJECT_NAME variable when the program is called to make a Test Driven Development any time you want. For example typing this command in the terminal in the folder where makePythonTdd.sh is saved makes a Test Driven Development environment for a project with the name person

    ./makePythonTdd.sh person
    

BONUS: makePythonTdd.sh Plus

Since you are the adventurous type and made it this deep in the book, I have added extra lines that

  • make the project with PROJECT_NAME as the name if you do not give a name

  • make the class name in CapWords format

  • open the test file and source file in your editor

 1#!/bin/bash
 2if [ -z "$1" ] ; then
 3    PROJECT_NAME="PROJECT_NAME"
 4else
 5    PROJECT_NAME=$1
 6fi
 7
 8IFS='_' read -r -a words <<< "$PROJECT_NAME"
 9IFS=''
10CLASS_NAME="${words[*]^}"
11
12mkdir -p $PROJECT_NAME/{src,tests}
13cd $PROJECT_NAME
14touch src/$PROJECT_NAME.py tests/__init__.py
15
16echo "import unittest
17
18
19class Test$CLASS_NAME(unittest.TestCase):
20
21    def test_failure(self):
22        self.assertFalse(True)
23
24
25# Exceptions seen
26# AssertionError
27" > tests/test_$PROJECT_NAME.py
28
29code src/$PROJECT_NAME.py
30code tests/test_$PROJECT_NAME.py
31
32python3 -m venv .venv
33source .venv/bin/activate
34python3 -m pip install --upgrade pip
35echo "pytest-watch" > requirements.txt
36python3 -m pip install --requirement requirements.txt
37pytest-watch

this works with Visual Studio Code you can change it to use the command for any Integrated Development Environment (IDE) you like