makePythonTdd

makePythonTdd with no variables

  • Here is the program from how to make a Python Test Driven Development environment automatically

     1#!/bin/bash
     2uv init more_magic
     3cd more_magic
     4mkdir src
     5mv main.py src/more_magic.py
     6mkdir tests
     7touch tests/__init__.py
     8
     9echo "import unittest
    10
    11
    12class TestMoreMagic(unittest.TestCase):
    13
    14    def test_failure(self):
    15        self.assertFalse(True)
    16
    17
    18# Exceptions seen
    19# AssertionError
    20" > tests/test_more_magic.py
    21
    22echo "pytest" > requirements.txt
    23echo "pytest-watcher" >> requirements.txt
    24uv add --requirement requirements.txt
    25uv run pytest-watcher . --now
    
     1uv init more_magic
     2cd more_magic
     3mkdir src
     4Move-Item "main.py" "src/more_magic.py"
     5mkdir tests
     6New-Item tests/__init__.py
     7
     8"import unittest
     9
    10
    11class TestMoreMagic(unittest.TestCase):
    12
    13    def test_failure(self):
    14        self.assertFalse(True)
    15
    16
    17# Exceptions seen
    18# AssertionError
    19" | Out-File "tests/test_more_magic.py" -Encoding UTF8
    20
    21"pytest" | Out-File requirements.txt -Encoding UTF8
    22"pytest-watcher" >> requirements.txt
    23uv add --requirement requirements.txt
    24uv run pytest-watcher . --now
    
  • change more_magic to the name of your Project and when the program is called to make a Test Driven Development any time you want

  • type this in the terminal to run the program

    ./makePythonTdd.sh
    
    ./makePythonTdd.ps1
    

makePythonTdd with variables

  • Here is the program from how to make a Python test driven development environment 3

     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-watcher" > requirements.txt
    27python3 -m pip install --requirement requirements.txt
    28pytest-watcher
    
    • use chmod to make the program executable

      chmod +x makePythonTdd.sh
      
     1$PROJECT_NAME=$args[0]
     2mkdir $PROJECT_NAME
     3cd $PROJECT_NAME
     4mkdir src
     5New-Item "src/$PROJECT_NAME.py"
     6mkdir tests
     7New-Item tests/__init__.py
     8
     9"import unittest
    10
    11
    12class Test$($PROJECT_NAME)(unittest.TestCase):
    13
    14    def test_failure(self):
    15        self.assertFalse(True)
    16
    17
    18# Exceptions seen
    19# AssertionError
    20" | Out-File "tests/test_$PROJECT_NAME.py" -Encoding UTF8
    21
    22python -m venv .venv
    23.venv/scripts/activate.ps1
    24python -m pip install --upgrade pip
    25"pytest-watcher" | Out-File requirements.txt -Encoding UTF8
    26python -m pip install --requirement requirements.txt
    27pytest-watcher
    
  • 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 the program is saved makes a Test Driven Development environment for a project with the name person

    ./makePythonTdd.sh person
    
    ./makePythonTdd.ps1 person
    

BONUS: makePythonTdd.sh Pro

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
    12uv init $PROJECT_NAME
    13mkdir -p $PROJECT_NAME/{src,tests}
    14cd $PROJECT_NAME
    15mv main.py src/$PROJECT_NAME.py
    16touch tests/__init__.py
    17
    18echo "import unittest
    19
    20
    21class Test$CLASS_NAME(unittest.TestCase):
    22
    23    def test_failure(self):
    24        self.assertFalse(True)
    25
    26
    27# Exceptions seen
    28# AssertionError
    29" > tests/test_$PROJECT_NAME.py
    30
    31code src/$PROJECT_NAME.py
    32code tests/test_$PROJECT_NAME.py
    33
    34echo "pytest" > requirements.txt
    35echo "pytest-watcher" >> requirements.txt
    36uv add --requirement requirements.txt
    37uv run pytest-watcher . --now
    
     1$PROJECT_NAME=$args[0]
     2uv init $PROJECT_NAME
     3cd $PROJECT_NAME
     4mkdir src
     5Move-Item "main.py" "src/$PROJECT_NAME.py"
     6mkdir tests
     7New-Item tests/__init__.py
     8
     9"import unittest
    10
    11
    12class Test$($PROJECT_NAME)(unittest.TestCase):
    13
    14    def test_failure(self):
    15        self.assertFalse(True)
    16
    17
    18# Exceptions seen
    19# AssertionError
    20" | Out-File "tests/test_$PROJECT_NAME.py" -Encoding UTF8
    21
    22code src/$PROJECT_NAME.py
    23code tests/test_$PROJECT_NAME.py
    24
    25"pytest" | Out-File requirements.txt -Encoding UTF8
    26"pytest-watcher" >> Out-File requirements.txt
    27uv add --requirement requirements.txt
    28uv run pytest-watcher . --now
    

code src/$PROJECT_NAME.py and code tests/test_$PROJECT_NAME.py work with Visual Studio Code you can change code to use the command for any Integrated Development Environment (IDE) you like