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 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.ps1 when I want it to make a project

     1$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-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$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-watch
    
  • I run the program again in the terminal

    ./makePythonTdd.ps1
    

    the 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.py to make the test pass

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

  • I deactivate the virtual environment

    deactivate
    

    the terminal goes back to the command line

  • I leave the more_magic folder to go back to the pumping_python folder

    cd ..
    

    the terminal shows

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

    tree /F
    

    the terminal shows

    .
    ├── magic
       ├── .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 $args[0] in PowerShell, it represents the first argument given when a program is called. For example,

    command argument
    

    in the code above, command will be makePythonTdd.ps1 and $args[0] will be argument or whatever name I give.

    Here are a few other examples

    mkdir name_of_folder
    

    mkdir is the command, and $args[0] is name_of_folder

    New-Item name_of_file
    

    New-Item is the command, and $args[0] is name_of_file

    echo "anything"
    

    echo is the command, and $args[0] is "anything"

    tree /F
    

    tree is the command and $args[0] is /F

  • I change more_magic to $args[0] in makePythonTdd.ps1

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

    ./makePythonTdd.ps1 more_magic
    

    the 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.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 deactivate the virtual environment

    deactivate
    

    the terminal goes back to the command line

  • I leave the more_magic folder to go back to the pumping_python folder

    cd ..
    

    the terminal shows

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

    tree /F
    

    the 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
        └── .venv
    
  • I can now make a Test Driven Development environment with makePythonTdd.ps1 when I give it a name for the PROJECT_NAME variable. For example, when I type this in the terminal

    ./makePythonTdd.ps1 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 Windows computer without Windows Subsystem for Linux.

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

Would you like to test AssertionError?


code from the chapter

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


please leave a review