IP Challenge: Part 1

I’m back! and I have brought the newbies out there a very simple challenge so you can learn just a bit about python classes, iteration, modules, and whatnot.

I have setup a repository called IPChallenge, here I will give you a full walkthrough but I encourage you to take on the challenge and don’t read the solutions given here.

To be honest the challenge is really simple, it is made for newbies out there after all, so go ahead and take the challenge, you’ll learn about code coverage and setting up a CI too.

Set up the challenge environment

Requirements

You will need the following toolset:

  • Python 3
  • Pipenv
  • Flake8
  • Pylint
  • PyDocStyle
  • Requests
  • Coverage

Fork and Clone the repository

First of all, go ahead and fork the repository on https://github.com/franccesco/ipchallenge

$ git clone [email protected]:your_forked_repo/ipchallenge.git

Install Pipenv

Pipenv will install a virtual environment and will aid us to manage our packages. It’s O.K if you want to use another dependency manager, for me pipenv works best.

$ pip install pipenv

Install the project dependencies

This will install the project requirements and development requirements.

pipenv install -d

We have now completed our set up, now we will take on Challenge #1 just as a freebie, this way you can see how the challenge work and when you have passed a challenge.

Starting The Challenges

Challenges are inside the folder test, they’re Unit Tests, these tests will fail if you haven’t met the requirements for making them pass, let’s start with the first one, shall we?

First test How-To

Now that we have set up our environment, we will start the first challenge and I’ll teach you how to pass the test. Open your terminal and go to the project’s root folder.

.
├── LICENSE
├── Pipfile
├── Pipfile.lock
├── README.md
└── tests
├── test_challenge_10.py
├── test_challenge_1.py
├── test_challenge_2.py
├── test_challenge_3.py
├── test_challenge_4.py
├── test_challenge_5.py
├── test_challenge_6.py
├── test_challenge_7.py
├── test_challenge_8.py
└── test_challenge_9.py

You’ll see a total of 10 challenges that needs to be completed.

On the root folder we will invoke Python with a special module called unittest and point it at our first challenge: test_challenge_1.py. Don’t forget to run python with pipenv run!

$ pipenv run python -m unittest tests/test_challenge_1.py -f
F
======================================================================
FAIL: test_create_module_folder (tests.test_challenge_1.TestModules)
Test the project architecture
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/franccesco/workspace/iptest/tests/test_challenge_1.py", line 19, in test_create_module_folder
msg="Directory 'modules' is missing")
AssertionError: False is not true : Directory 'modules' is missing

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (failures=1)

Aaand, we fail already, but this is expected. Each challenge will have instructions that will guide you on how to make a test pass, additionally a failing test will have a message for you, and you should keep an eye on those messages because their very useful hints and sometimes will often give you the answers.

Making a test pass

We have to read the failing message first:

AssertionError: False is not true : Directory 'modules' is missing

The first test is about to set up a very simple project structure. we will have a very simplistic project structure which will consist of only a folder where we will be writing our project’s modules. It seems that the directory modules is missing, we have to create it and then run our test again:

$ mkdir modules
$ pipenv run python -m unittest tests/test_challenge_1.py -f
-- SNIP --
AssertionError: False is not true : The file __init__.py was not found in'modules/'
-- SNIP --

Now the test is not complaining about the folder modules, it’s even aware of its existence, it is now complaining that it cannot find the file __init__.py under the modules directory. Let’s also fix this:

$ touch modules/__init__.py
$ pipenv run python -m unittest tests/test_challenge_1.py -f
-- SNIP --
AssertionError: False is not true : The file ipinfo.py was not found in'modules/'
-- SNIP --

Now let’s create the file ipinfo.py and re-run the test:

$ touch modules/ipinfo.py
$ pipenv run python -m unittest tests/test_challenge_1.py -f
.
----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

Aaand that’s it! We passed our first test, really easy right? I made the first test easy so you can get the feeling of how to check if you pass or fail a test.

Luckily we I will be completing this walktrough in 4 or 5 posts so I can explain to you what is happening in each test, how to setup your code coverage, CI, import modules, etc.

Have fun with the challenge.