how to make a Python Test Driven Development environment automatically on Windows without Windows Subsystem for Linux


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 27 lines of code, with spaces

 1mkdir more_magic
 2cd more_magic
 3mkdir src
 4New-Item "src/more_magic.py"
 5mkdir tests
 6New-Item tests/__init__.py
 7
 8"import unittest
 9
10
11class TestMoreMagic(unittest.TestCase):
12
13    def test_failure(self):
14        self.assertFalse(True)
15
16
17# Exceptions seen
18# AssertionError
19" | Out-File "tests/test_more_magic.py" -Encoding UTF8
20
21python -m venv .venv
22.venv/scripts/activate.ps1
23python -m pip install --upgrade pip
24"pytest-watcher" | Out-File requirements.txt -Encoding UTF8
25python -m pip install --requirement requirements.txt
26pytest-watcher

questions about making a Python Test Driven Development Environment automatically on Windows without Windows Subsystem for Linux

Here are questions you can answer after going through this chapter


how to make a PowerShell script

  • I go to the terminal and use New-Item 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

    New-Item makePythonTdd.ps1
    

    the terminal goes back to the command line

  • I open makePythonTdd.ps1 in the editor of the Integrated Development Environment (IDE), then add the commands I use to make a Python Test Driven Development environment for a project

     1mkdir magic_again
     2cd magic_again
     3mkdir src
     4New-Item src/magic_again.py
     5mkdir tests
     6New-Item tests/__init__.py
     7New-Item tests/test_magic_again.py
     8python -m venv .venv
     9.venv/scripts/activate.ps1
    10python -m pip install --upgrade pip
    11"pytest-watcher" | Out-File requirements.txt -Encoding UTF8
    12python -m pip install --requirement requirements.txt
    13pytest-watcher
    
  • test_magic_again.py is an empty file becuase I used New-Item. 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 Out-File to add the text to test_magic_again.py when it makes the file in the tests folder, the same I do with the requirements.txt file

     5mkdir tests
     6New-Item tests/__init__.py
     7
     8"" | Out-File "tests/test_magic.py" -Encoding UTF8
     9
    10python -m venv .venv
    11.venv/scripts/activate.ps1
    
  • I add the text for the test inside the quotes (“”) I just added to makePythonTdd.sh, the way I do with Out-File when I add "pytest-watcher" as text in requirements.txt

    Caution

    Indentation is important in Python, I use 4 spaces as convention in this book, see the Python Style Guide for more

     8"import unittest
     9
    10
    11class TestMagicAgain(unittest.TestCase):
    12
    13    def test_failure(self):
    14        self.assertFalse(True)
    15
    16
    17# Exceptions seen
    18# AssertionError
    19" | Out-File "tests/test_magic_again.py" -Encoding UTF8
    

how to run a PowerShell script

  • I go back to the terminal to run the program

    makePythonTdd.ps1
    

    the terminal shows

    command not found: makePythonTdd.ps1
    

    I have to tell the computer where the file is

    .\makePythonTdd.ps1
    

    .\ is shorthand for this directory which in this case is pumping_python where makePythonTdd.ps1 is saved. The 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_again project 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.py to open it in the editor then make the test pass

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

    deactivate
    

    the terminal goes back to the command line

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

    cd ..
    

    the terminal shows

    ...\pumping_python >
    
  • I want to use makePythonTdd.ps1 to make another project with a different name. I change magic_again to the name of the new project in the editor

    Note

    The lines that are changing in the code are highlighted

     1mkdir more_magic
     2cd more_magic
     3mkdir src
     4touch src/more_magic.py
     5mkdir tests
     6touch tests/__init__.py
     7
     8"import unittest
     9
    10
    11class TestMoreMagic(unittest.TestCase):
    12
    13    def test_failure(self):
    14        self.assertFalse(True)
    15
    16
    17# Exceptions seen
    18# AssertionError
    19" | Out-File "tests/test_more_magic.py" - Encoding UTF8
    
  • I run makePythonTdd.ps1 in the terminal to make a project named more_magic

    ./makePythonTdd.ps1
    

    the 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 click in the terminal and use q on the keyboard to leave the tests and the terminal goes back to the command line

the program works and can make a Python Test Driven Development environment automatically the way I want every time


close the project

  • I close test_more_magic.py in the editor I had open

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

    deactivate
    

    the terminal goes back to the command line, (.venv) is no longer on the left side

    ...\pumping_python\more_magic
    
  • I change directory to the parent of more_magic

    cd ..
    

    the terminal shows

    ...\pumping_python
    

    I am back in the pumping_python directory


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

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


Click Here to see the code for the program to make a Python Test Driven Development environment for any Linux or MacOS computers


what is next?

you have gone through a few things

Would you like to use makePythonTdd.ps1 to make a Python Test Driven Development environment to test that an Exception is raised?


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