This is a description of the initial setup under Windows allowing me to follow along with the O’Reilly book ‘Test-Driven Development with Python’ which I'm currently reviewing.
I’d have done all my TDD with Python work under Ubunutu/Docker if my trusty little Atom-based machine hadn’t gone west recently (just a fan died, but I still haven’t gotten around to replacing that special sized fan).
So I decided to test natively on a Windows8 machine - this would be a good opportunity to test that the book doesn’t depend on Linux.
Linux
In the meantime, due to some strange Windows connection problems I also tried the code on a Linux Mint system just to be sure. No problems there. I also managed to workaround the connection problems seen on Windows, described later.
Linux
In the meantime, due to some strange Windows connection problems I also tried the code on a Linux Mint system just to be sure. No problems there. I also managed to workaround the connection problems seen on Windows, described later.
PythonAnywhere
Note: Appendix A of the book describes how to use the PythonAnywhere, a PaaS (Platform as a Service), allowing you to run the code in the cloud in a Python-hosting environment. I did try this but unfortunately couldn't figure out how to combine the site-generated web app and my github repository. It's unfortunate that the appendix isn't more explicit about how to do this.
Note: Appendix A of the book describes how to use the PythonAnywhere, a PaaS (Platform as a Service), allowing you to run the code in the cloud in a Python-hosting environment. I did try this but unfortunately couldn't figure out how to combine the site-generated web app and my github repository. It's unfortunate that the appendix isn't more explicit about how to do this.
Upgrading Anaconda to Python3
On Windows I work from a Cygwin shell, but decided I wanted to use my Anaconda installation.
Well first of all I realized my Anaconda was running Python 2.7.6, not Python3 which the book, quite rightly, uses. A bit of Googling allowed me to install Python3 under Anaconda as a separate environment, as described here.
So I installed a Python3 environment into Anaconda using (bare in mind I’m using Cygwin so I have Unix-style PATHs here):
cd /c/Progs/Anaconda/
conda create -n py3k python=3 anaconda
The Anaconda activate script wasn’t doing what I needed - in my Cygwin environment - so I decided to explicitly add appropriate paths to my PATH environment variable by adding the following lines at the end of my .profile:
echo "Adding Anaconda python3 to PATH" >&2
export PATH=/c/Progs/Anaconda/envs/py3k:$PATH
export PATH=/c/Progs/Anaconda/envs/py3k/Scripts:$PATH
Now source your .profile and verify that python -V produces something like:
Python 3.3.5 :: Anaconda 2.0.1 (64-bit)
Note: The Anaconda installation installs Python3 as python.exe and Pip as pip.exe, which could be
confusing as the book refers to python3 and pip3 as executeables.
First functional test run
At this point I ran the first version of the functional_test.py script described on page 5 of the book.
This failed because I hadn’t installed selenium yet.
So a quick
pip install selenium
instantly fixed this.
I reran the test which failed because it wasn’t seeing my ‘Portable Firefox’.
I Googled to see how to point Selenium to my Portable Firefox but decided it would be much easier to just install a standard Firefox, so I did that.
I reran the test which now opened a new Firefox window, and then subsequently failed because I didn’t have Django installed yet, this had been described in the book pre-requisites.
Installing Django 1.7
As described in the book, if django 1.7 isn’t yet available, the following command will fail
pip install django==1.7
and so we need to install from another source. Unfortunately the books’ link to django1.7 under github was no longer valid (hey, this is the ‘wibbly wobbly web’ and so my link below probably won’t work for you either … but with a bit of Googling you’ll get there!)
I performed a pip install to install a pre-release Django 1.7 as described on the download page:
pip install https://www.djangoproject.com/download/1.7c3/tarball/
which installed Django 1.7 release candidate3 without problem.
Update: Django 1.7 is now released.
So
pip install django==1.7
is now sufficient.
Second functional test run
I then reran the test and got the expected error as described on page 5, because Django wasn’t yet up and running.
$ python functional_tests.py
Traceback (most recent call last):
File "functional_tests.py", line 7, in <module>
assert 'Django' in browser.title
AssertionError
I tried the
django-admin.py startproject superlists
command from page 6 of the book, but this complained that the file couldn’t be found (maybe due to me using Cygwin?), noticing a file django-admin.exe in the Anaconda Scripts directory of my Python3 environment I decided to try that:
django-admin.exe startproject superlists
returned instantly and I saw that the project had been created locally in sub directory superlists.
I then started my new Django project:
cd superlists
python manage.py runserver
and then relaunched the functional tests. This time the browser opened displaying the Django welcome page “It worked!”. The functional test now succeeded because it correctly detected the word Django in the browser page.
Up and Running
This got me operational for running the initial tests in the book.
I’ll update this blog post later with any further steps such as MongoDB installation.
Workaround for Windows connection problems
During chapter 6 the functional tests start to create a second firefox session.
At this point I started getting some connection errors under Windows.
I found that inserting a print statement at the end of each of the two browser sessions prevented this error from occuring !
However, later in the book (you see the book really covers everything you need !!) it's mentioned that it's sufficient to insert a self.browser.refresh() before .quit(). I tried that before the .quit() in my functional_tests and at the end of the test script and the problem went away - without the need for any print statements !
At this point I started getting some connection errors under Windows.
File "C:\Progs\Anaconda\envs\py3k\lib\socket.py", line 426, in create_connection
sock.connect(sa)
ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it
For a while I just put up with this, but I decided to insert some print statements into my functional_tests/test.py file to determine at what point the connection error was occuring.I found that inserting a print statement at the end of each of the two browser sessions prevented this error from occuring !
However, later in the book (you see the book really covers everything you need !!) it's mentioned that it's sufficient to insert a self.browser.refresh() before .quit(). I tried that before the .quit() in my functional_tests and at the end of the test script and the problem went away - without the need for any print statements !
No comments:
Post a Comment