how to make a python test driven development environment on Windows without Windows Subsystem for Linux with variables
makePythonTdd.ps1 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 PowerShell script
I changed magic 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 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 add a name to represent any project name that I give to
makePythonTdd.ps1when I want it to make a project1$PROJECT_NAME="more_magic" 2mkdir more_magic 3cd more_magic 4mkdir src 5touch src/more_magic.py 6mkdir tests 7touch tests/__init__.py 8 9"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" | Out-File "tests/test_more_magic.py" - Encoding UTF8 21 22python3 -m venv .venv 23source .venv/bin/activate 24python3 -m pip install --upgrade pip 25"pytest-watch" | Out-File requirements.txt -Encoding UTF 8 26python3 -m pip install --requirement requirements.txt 27pytest-watcha variable is a name that is used for a value that can change. For example, we use the word
womanto represent any woman,manto represent any man,childto represent any child, andparentto represent anyone with a child. In this case I use$PROJECT_NAMEto represent any name of a projectI change every where I have
more_magicin 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$PROJECT_NAME="more_magic" 2mkdir $PROJECT_NAME 3cd $PROJECT_NAME 4mkdir src 5touch src/$PROJECT_NAME.py 6mkdir tests 7touch 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 22python3 -m venv .venv 23source .venv/bin/activate 24python3 -m pip install --upgrade pip 25"pytest-watch" | Out-File requirements.txt -Encoding UTF 8 26python3 -m pip install --requirement requirements.txt 27pytest-watchI run the program again in the terminal
./makePythonTdd.ps1the terminal shows
======================================= FAILURES ======================================= ____________________________ Testmore_magic.test_failure ______________________________ self = <tests.test_magic.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.pyto make the test passI hit ctrl+c in the terminal to stop the test
I deactivate the virtual environment
deactivatethe terminal goes back to the command line
I leave the
more_magicfolder to go back to thepumping_pythonfoldercd ..the terminal shows
...\pumping_pythonI run tree to see what I have in the
pumping_pythonfolder nowtree /Fthe terminal shows
. ├── magic │ ├── .pytest_cache │ ├── requirements.txt │ ├── src │ ├── tests │ └── .venv ├── more_magic │ ├── .pytest_cache │ ├── requirements.txt │ ├── src │ ├── tests │ └── .venv └── makePythonTdd.shThe 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
$args[0]in PowerShell, it represents the first argument given when a program is called. For example,command argumentin the code above, command will be
makePythonTdd.ps1and$args[0]will beargumentor whatever name I give.Here are a few other examples
mkdir name_of_foldermkdiris the command, and$args[0]isname_of_folderNew-Item name_of_fileNew-Itemis the command, and$args[0]isname_of_fileecho "anything"echois the command, and$args[0]is"anything"tree /Ftreeis the command and$args[0]is/FI change
more_magicto$args[0]inmakePythonTdd.ps11$PROJECT_NAME=$args[0]I try the program again, this time with a different name for the project in the terminal
./makePythonTdd.ps1 more_magicthe terminal shows
======================================= FAILURES ======================================= _____________________________ Testmore_magic.test_failure ______________________________ self = <tests.test_more_magic.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 0.04s ===================================I hold ctrl on the keyboard in the terminal and click on
tests/test_more_magic.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 deactivate the virtual environment
deactivatethe terminal goes back to the command line
I leave the
more_magicfolder to go back to thepumping_pythonfoldercd ..the terminal shows
...\pumping_python >I run tree to see what I have in the
pumping_pythonfoldertree /Fthe terminal shows
. ├── magic │ ├── .pytest_cache │ ├── requirements.txt │ ├── src │ ├── tests │ └── .venv ├── more_magic │ ├── .pytest_cache │ ├── requirements.txt │ ├── src │ ├── tests │ └── .venv ├── makePythonTdd.sh └── more_magic ├── .pytest_cache ├── requirements.txt ├── src ├── tests └── .venvI can now make a Test Driven Development environment with
makePythonTdd.ps1when I give it a name for thePROJECT_NAMEvariable. For example, when I type this in the terminal./makePythonTdd.ps1 assertion_errorthe 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 Windows computer without Windows Subsystem for Linux.
How many questions can you answer after going through this chapter?