how to make a Python Test Driven Development environment automatically with variables


makePythonTdd.sh works and always makes a Python Test Driven Development environment the way I want it, but there is a problem

how to use a variable in a shell script

I changed magic_again to more_magic in 5 places in makePythonTdd.sh. I would have to do the same change every time I have a new project, and I want to follow The Do Not Repeat Yourself (DRY) Principle. I want the program to take a project name once and use that name when making the project to make the following

The program should always make this structure

PROJECT_NAME
├── requirements.txt
├── src
   └── PROJECT_NAME.py
├── tests
   ├── __init__.py
   └── test_PROJECT_NAME.py
└── .venv

Time to use a variable for the name of the project

  • I add a name to represent any project name that I give to makePythonTdd.sh when I want it to make a project

     1#!/bin/bash
     2PROJECT_NAME="more_magic"
     3mkdir more_magic
     4cd more_magic
     5mkdir src
     6touch src/more_magic.py
     7mkdir tests
     8touch tests/__init__.py
     9
    10echo "import unittest
    11
    12
    13class TestMagicAgain(unittest.TestCase):
    14
    15    def test_failure(self):
    16        self.assertFalse(True)
    17
    18
    19# Exceptions seen
    20# AssertionError
    21" > tests/test_more_magic.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
    

    a variable is a name that is used for a value that can change. For example, we use the word woman to represent any woman, man to represent any man, child to represent any child, and parent to represent anyone with a child. In this case I use $PROJECT_NAME to represent any name of a project

  • I change every where I have more_magic in the program, to use the variable I just added so that I only have to make a change in one place

    Note

    The lines that are changing in the code are highlighted

     1#!/bin/bash
     2PROJECT_NAME="more_magic"
     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
    
  • I run the program again in the terminal

    ./makePythonTdd.sh
    

    the terminal shows

    ======================================= FAILURES =======================================
    ____________________________ Testmore_magic.test_failure ______________________________
    
    self = <tests.test_magic_again.Testmore_magic testMethod=test_failure>
    
        def test_failure(self):
    >       self.assertFalse(True)
    E       AssertionError: True is not false
    
    tests/test_more_magic.py:7: AssertionError
    =============================== short test summary info ================================
    FAILED tests/test_more_magic.py::Testmore_magic::test_failure - AssertionError: True is not false
    ================================== 1 failed in X.YZs ===================================
    
  • I change True to False in tests/test_more_magic.py to make the test pass

  • I hit ctrl+c in the terminal to stop the test

  • I run tree to see what I have in the pumping_python folder now

    tree -a -L 2
    

    the terminal shows

    .
    ├── magic_again
       ├── .pytest_cache
       ├── requirements.txt
       ├── src
       ├── tests
       └── .venv
    ├── more_magic
       ├── .pytest_cache
       ├── requirements.txt
       ├── src
       ├── tests
       └── .venv
    └── makePythonTdd.sh
    
  • The program works as expected, and I only need to give the project name in one place. It would be nice if I do not have to go into the file to give it the project name. I want to be able to just call the program and give it a name for the project from the command line. I can do this with $1 in bash, it represents the first argument given when a program is called. For example,

    command argument
    

    in the code above, command will be ./makePythonTdd.sh and $1 will be argument or whatever name I give.

    Here are a few other examples

    mkdir name_of_folder
    

    mkdir is the command, and $1 is name_of_folder

    touch name_of_file
    

    touch is the command, and $1 is name_of_file

    echo "anything"
    

    echo is the command, and $1 is "anything"

    tree -a
    

    tree is the command and $ is -a

  • I change more_magic to $1 in makePythonTdd.sh

    2PROJECT_NAME=$1
    
  • I try the program again, this time with a different name for the project in the terminal

    ./makePythonTdd.sh more_magic_again
    

    the terminal shows

    ======================================= FAILURES =======================================
    _____________________________ Testmore_magic_again.test_failure ______________________________
    
    self = <tests.test_more_magic_again.Testmore_magic_again testMethod=test_failure>
    
        def test_failure(self):
    >       self.assertFalse(True)
    E       AssertionError: True is not false
    
    tests/test_more_magic_again.py:7: AssertionError
    =============================== short test summary info ================================
    FAILED tests/test_more_magic_again.py::Testmore_magic_again::test_failure - AssertionError: True is not false
    ================================== 1 failed in 0.04s ===================================
    
  • I hold ctrl on the keyboard in the terminal and click on tests/test_more_magic_again.py to open it in the editor, then make the test pass

  • I use ctrl+c on the keyboard in the terminal to stop the tests

  • I run tree to see what I have in the pumping_python folder

    tree -a -L 2
    

    the terminal shows

    .
    ├── magic_again
       ├── .pytest_cache
       ├── requirements.txt
       ├── src
       ├── tests
       └── .venv
    ├── more_magic
       ├── .pytest_cache
       ├── requirements.txt
       ├── src
       ├── tests
       └── .venv
    ├── makePythonTdd.sh
    └── more_magic_again
        ├── .pytest_cache
        ├── requirements.txt
        ├── src
        ├── tests
        └── .venv
    
  • I can now make a Test Driven Development environment with makePythonTdd.sh when I give it a name for the PROJECT_NAME variable. For example, when I type this in the terminal

    ./makePythonTdd.sh assertion_error
    

    the terminal shows

    ====================================== FAILURES =======================================
    __________________________ Testassertion_error.test_failure ___________________________
    
    self = <tests.test_assertion_error.Testassertion_error testMethod=test_failure>
    
        def test_failure(self):
    >       self.assertFalse(True)
    E       AssertionError: True is not false
    
    tests/test_assertion_error.py:7: AssertionError
    =============================== short test summary info ===============================
    FAILED tests/test_assertion_error.py::Testassertion_error::test_failure - AssertionError: True is not false
    ================================== 1 failed in X.YZs ==================================
    

    the computer makes a Test Driven Development environment for a project called assertion_error and runs the first failing test. I continue this in AssertionError


review

Computer Programming allows me to take some steps and make them a one line command for the computer to do for me. You have seen a way to make a Python Test Driven Development environment, and have a program to do it for you on any Linux, Windows or MacOS computers.

How many questions can you answer after going through this chapter?


code from the chapter

Do you want to see all the CODE I typed in this chapter?


Would you like to test AssertionError?


please leave a review