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


how to use a variable in a shell script

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

I change the name of the project in 5 places every time I use the program to make a project. 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 open makePythonTdd.sh in the editor

    Tip

    Here is a quick way to open makePythonTdd.sh if you are using Visual Studio Code

    code makePythonTdd.sh
    
  • 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="dictionaries"
    3mkdir dictionaries
    4cd dictionaries
    

    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

    • 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 dictionaries 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="dictionaries"
     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
    
  • Since I already did some work in the dictionaries project. I do not want the program to write over it. I change the name of the project to pro_magic because I am a professional

    1#!/bin/bash
    2PROJECT_NAME="pro_magic"
    3mkdir $PROJECT_NAME
    4cd $PROJECT_NAME
    
  • I run the program in the terminal

    ./makePythonTdd.sh
    

    the terminal shows

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

    this program does not make the class names in the CapWords format so they are in snake_case when made but there is a better way

  • I hold ctrl on the keyboard and click on tests/pro_magic.py in the terminal to open it in the editor

  • then I change True to False in the assertion

    7        self.assertFalse(False)
    

    the test passes

  • I click in the terminal and use q on the keyboard to leave the tests and the terminal goes back to the command line

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

    tree -a -L 1 pro_magic
    

    the terminal shows

    pro_magic
    ├── .pytest_cache
    ├── requirements.txt
    ├── src
    ├── tests
    └── .venv
    

The program does what I want, 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.


how to call a shell script with arguments

I want to be able to 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 is the name of the program and $1 is argument

Here are a few other examples

mkdir folder_name

mkdir is the command, and $1 is folder_name

touch file_name

touch is the command, and $1 is file_name

echo "echo"

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

tree -a

tree is the command $1 is -a


  • I change pro_magic to $1 in makePythonTdd.sh

    1#!/bin/bash
    2PROJECT_NAME=$1
    3mkdir $PROJECT_NAME
    
  • I try the program again, this time with a name for the project in the terminal

    ./makePythonTdd.sh pro_magic_plus
    

    the terminal shows

    ================================= FAILURES =================================
    ____________________ Testpro_magic_plus.test_failure _______________________
    
    self = <tests.test_pro_magic_plus.Testpro_magic_plus testMethod=test_failure>
    
        def test_failure(self):
    >       self.assertFalse(True)
    E       AssertionError: True is not false
    
    tests/test_pro_magic_plus.py:7: AssertionError
    ========================= short test summary info ==========================
    FAILED tests/test_pro_magic_plus.py::Testpro_magic_plus::test_failure - AssertionError: True is not false
    ============================ 1 failed in X.YZs =============================
    
  • I hold ctrl on the keyboard in the terminal and click on tests/test_pro_magic_plus.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 pro_magic_plus folder

    tree -a -L 1 pro_magic_plus
    

    the terminal shows

    pro_magic_plus
    ├── .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 person
    

    the terminal shows

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

the computer makes a Python Test Driven Development environment for a project called person and runs the first failing test. I continue this in how to make a person


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.


code from the chapter

Do you want to see the CODE for makePythonTdd.sh?


what is next?

you know

would you like to test using dictionaries and functions to make a person?


rate pumping python

If this has been a 7 star experience for you, please CLICK HERE to leave a 5 star review of pumping python. It helps other people get into the book too