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.
You will need the following toolset:
- Python 3
First of all, go ahead and fork the repository on https://github.com/franccesco/ipchallenge
$ git clone [email protected]:your_forked_repo/ipchallenge.git
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
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.
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?
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.
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 python -m unittest tests/test_challenge_1.py -f
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.
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
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
Now let’s create the file
ipinfo.py and re-run the test:
$ touch modules/ipinfo.py
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.