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 project folder
the test class in the test file
the virtual environment in the
.venvfolder
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.shin the editorTip
Here is a quick way to open
makePythonTdd.shif you are using Visual Studio Codecode makePythonTdd.shI add a name to represent any project name that I give to
makePythonTdd.shwhen I want it to make a project1#!/bin/bash 2PROJECT_NAME="dictionaries" 3mkdir dictionaries 4cd dictionariesa variable is a name that is used for a value that can change. For example, we use the word
womanto represent any womanmanto represent any manchildto represent any childparentto represent anyone with a child.
In this case I use
$PROJECT_NAMEto represent any name of a projectI change every where I have
dictionariesin the program, to use the variable I just added so that I only have to make a change in one placeNote
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.pySince 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_magicbecause I am a professional1#!/bin/bash 2PROJECT_NAME="pro_magic" 3mkdir $PROJECT_NAME 4cd $PROJECT_NAMEI run the program in the terminal
./makePythonTdd.shthe 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.pyin the terminal to open it in the editorthen 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_magicfoldertree -a -L 1 pro_magicthe 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_magicto$1inmakePythonTdd.sh1#!/bin/bash 2PROJECT_NAME=$1 3mkdir $PROJECT_NAMEI try the program again, this time with a name for the project in the terminal
./makePythonTdd.sh pro_magic_plusthe 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.pyto open it in the editor, then make the test passI 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_plusfoldertree -a -L 1 pro_magic_plusthe terminal shows
pro_magic_plus ├── .pytest_cache ├── requirements.txt ├── src ├── tests └── .venvI can now make a Test Driven Development environment with
makePythonTdd.shwhen I give it a name for thePROJECT_NAMEvariable. For example, when I type this in the terminal./makePythonTdd.sh personthe 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
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