how to make a Python Test Driven Development environment automatically
To review, here are steps I take to make the environment for every project
I give the project a name
I make a Python file to hold the source code in the ‘src’ folder
I make a Python file to hold the tests in the ‘tests’ folder
I make the test pass
then I start working on the project
I want to give one command for the program to do every step except
give the project a name
make the test pass and
work on the project
I only want to do 3 steps not 18
preview
Here is the program_ I have by the end of the chapter to automatically make a python test driven development environment, it is only 28 lines of code, with spaces
1#!/bin/bash
2mkdir more_magic
3cd more_magic
4mkdir src
5touch 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
22python3 -m venv .venv
23source .venv/bin/activate
24python3 -m pip install --upgrade pip
25echo "pytest-watch" > requirements.txt
26python3 -m pip install --requirement requirements.txt
27pytest-watch
questions about making a Python Test Driven Development Environment automatically
Here are questions you can answer after going through this chapter
how to make a shell script
I go to the terminal_ and use touch_ to make an empty file_ with a name that is easy to remember later. I want the name to also describe the program_ that will automatically make a Test Driven Development environment for me
touch makePythonTdd.shthe terminal_ goes back to the command line
I open
makePythonTdd.shin the editor of the `Integrated Development Environment (IDE)`_, then add the commands I use to make a Python_ Test Driven Development environment for a project1#!/bin/bash 2mkdir magic_again 3cd magic_again 4mkdir src 5touch src/magic_again.py 6mkdir tests 7touch tests/__init__.py 8touch tests/test_magic_again.py 9python3 -m venv .venv 10source .venv/bin/activate 11python3 -m pip install --upgrade pip 12echo "pytest-watch" > requirements.txt 13python3 -m pip install --requirement requirements.txt 14pytest-watch#!/bin/bashis called a shebang_ line, it tells the computer to use bash_ to run this program_test_magic_again.pyis an empty file_ because I used touch_. I want it to have the text for the first failure so I do not have to open the editor to add the text for it in each project. I use echo_ instead of touch_ to make themakePythonTdd.shprogram_ add the text totest_magic_again.pywhen it makes the file_ in thetestsfolder_, the same I do with therequirements.txtfile_6mkdir tests 7touch tests/__init__.py 8 9echo "" > tests/test_magic_again.py 10 11python3 -m venv .venv 12source .venv/bin/activateI add the text for the test inside the quotes (“”) I just added to
makePythonTdd.sh, the way I do with echo_ when I add"pytest-watch"as text inrequirements.txtCaution
Indentation_ is important in Python_, I use 4 spaces as convention in this book, see the Python Style Guide for more
9echo "import unittest 10 11 12class TestMagicAgain(unittest.TestCase): 13 14 def test_failure(self): 15 self.assertFalse(True) 16 17 18# Exceptions seen 19# AssertionError 20" > tests/test_magic_again.py
how to run a shell script
I go back to the terminal_ to run the program_
makePythonTdd.sh
the terminal_ shows
command not found: makePythonTdd.sh
I have to tell the computer where the file_ is
./makePythonTdd.sh
./ is shorthand for this directory which in this case is pumping_python where makePythonTdd.sh is saved. The computer checks a few directories_ when a command is given. Those directories are where commands like mkdir_, cd_, tree_ and echo_ are saved. The terminal_ shows
permission denied: ./makePythonTdd.sh
I want to make sure the computer can run the program_. I have to make it executable
how to view the permissions of a file
I use ls_ to check the permissions of the file_
ls -l makePythonTdd.sh
-l is the option to show the long listing format which includes permissions for the file_
the terminal_ shows
-rw-r--r-- 1 abcdef ghijk XX Month Y ZA:BC makePythonTdd.sh
the first 10 characters above (-rw-r--r--) are grouped
- rw- r-- r--
the groups that have 3 characters show read, write and execute permissions with
rforreadwforwriteandxforexecute
here is what it means for makePythonTdd.sh
the first group tells if this is file_ or directory_ :
-means this is a regular file_, it is not a directory_this group shows permissions for the owner of the thing:
rw-means the owner of the file_ can read and write to the file_ but NOT execute itthis group shows permissions for the group of the owner of the thing:
r--means the group can read the file_, NOT write to it or execute itthis group is for other users:
r--means other users can read the file_, NOT write to it or execute it
I want to add execute permissions so I can run the file
how to make a shell script run as a command
I change the mode of the file_ to add executable permissions
chmod +x makePythonTdd.shchmod_ is a program_ that changes the mode (permissions) of the given file_, the terminal_ goes back to the command line
I list the permissions again with ls_
ls -l makePythonTdd.shthe terminal_ shows
-rwxr-xr-x 1 abcdef ghijk XX Month Y ZA:BC makePythonTdd.shhere is what each one of the characters before the folder_ means
-means this is a regular file_, it is not a directory_rwxmeans the owner of the file_ has permissions to read, write to and execute itr-xmeans the group the owner of the file_ belongs to has permissions to read and execute the file_ they cannot write to it, and the secondr-xmeans other users have permissions to read and execute the file_, they cannot write to it
I try the command again
./makePythonTdd.shthe terminal_ shows
======================================= FAILURES ======================================= _____________________________ TestMagicAgain.test_failure ______________________________ self = <tests.test_magic_again.TestMagicAgain testMethod=test_failure> def test_failure(self): > self.assertFalse(True) E AssertionError: True is not false tests/test_magic_again.py:7: AssertionError =============================== short test summary info ================================ FAILED tests/test_magic_again.py::TestMagicAgain::test_failure - AssertionError: True is not false ================================== 1 failed in X.YZs ===================================Success! I just made a program_ that can make the
magic_againproject anytime I want and it automatically does the steps I did manually.I hold ctrl on the keyboard and click on
tests/test_magic_again.pyto open it in the editor then make the test passI hit ctrl+c in the terminal_ to stop the test
I want to use
makePythonTdd.shto make another project with a different name. I changemagic_againto the name of the new project in the editorNote
The lines that are changing in the code are highlighted
1#!/bin/bash 2mkdir more_magic 3cd more_magic 4mkdir src 5touch 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.pyI run
makePythonTdd.shin the terminal_ to make a project namedmore_magic./makePythonTdd.shthe terminal_ shows AssertionError
======================================= FAILURES ======================================= ______________________________ TestMoreMagic.test_failure ______________________________ self = <tests.test_more_magic.TestMoreMagic 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::TestMoreMagic::test_failure - AssertionError: True is not false ================================== 1 failed in X.YZs ===================================I make the test pass
I hit ctrl+c to exit the tests in the terminal_
the program_ works and can make a Python_ Test Driven Development environment automatically the way I want every time
close the project
I close the
test_more_magic.pyin the editor I had openI click in the terminal_ and exit the tests with ctrl+c on the keyboard
I `change directory`_ to the parent of
more_magiccd ..the terminal_ shows
.../pumping_pythonI am back in the
pumping_pythondirectory_
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 I can make a Python_ Test Driven Development environment, with a program to do it on any Linux_, Windows_ or MacOS_ computers.
How many questions can you answer after going through this chapter?
code from the chapter
what is next?
Here is what we have gone through together so far
rate pumping python
If this has been a 7 star experience for you, please leave a 5 star review. It helps other people get into the book too