DJANGO TEST DRIVEN DEVELOPMENT WITH BITBUCKET
Tags: Django, Bitbucket, Development, Continuous Integration, Testing

Posted by Brian Njoroge and DLMade.

Assuming you made you made a smoke detector alarm that triggers an alarm once it detects smoke, you cannot just start selling it directly or install it in your house waiting for the day you house catches fire to find out whether it works. You have to test its functionality. To make it simple, you would make two tests. One, ensure the alarm is not triggered when there is no smoke therefore raising false alarms. Two, ensure the alarm is triggered when there is smoke.

Just like the smoke detector, you cannot deploy a software to production without testing. According to me, undergraduate computer science courses hasten students to learn new programming languages every semester and don’t utilize enough time to teach them about software testing. Big IT companies have seen the need of testing as a major step in software development and emphasize on testing of softwares thoroughly before shipping them out.

In this article, we will emphasize on Test Driven Development(TDD) with Django and Continuous Development — Continuous Integration (CD-CI) with Bitbucket. So hop into the ship in this two part article.

PART 1: TDD WITH DJANGO

As websites grow they become harder to test manually. There is this old saying by Andrea Koutifaris “All code is guilty until proven innocent”. Test-driven development is a process of modifying the code in order to pass a test designed previously. Just like the smoke detector alarm analogy, we write deliberately failing test cases, we will also write application code to make the test cases pass.

For demonstration purposes, we will use this repo a simple Django user authentication project. Otherwise, you are free to implement this project template in your applications.

Step 1: Cloning the repo

To clone the repo, paste this command in your terminal:

git clone https://BriaNjoroge@bitbucket.org/BriaNjoroge/django-user-authentication.git

Step 2: Writing test cases

In the project folder, navigate to the core folder then to the tests folder. This is where I have written my test cases.

Django uses the unit test module’s built-in test discovery, which will discover tests under the current working directory in any file named with the pattern test*.py. Good rule of thumb, when doing unit tests its good to test the URLs, views, forms and models.

I have commented the code well for better understanding, its easier to explain.

Testing the urls:

The code is well document but in a nutshell the file tests that the associated urls lead to their associated views function.

Testing the Views

Testing the Forms

Step 3: Running the tests

In order to run the tests, we use the command below.

python manage.py test

At the moment, the repo has only 5 test functions which all pass. After running the command, the result accepted is as;

PART 2: CONTINOUS INTEGRATION WITH BITBUCKET PIPELINES

Linus Torvalds is a genius for having created git. Git version control system has been popular in system integrations especially when you working as a team. Many platforms have provided git services such as GitHub, Bitbucket and Gitlab.

Continuous integration (CI) is the practice of automating the integration of code changes from multiple contributors into a single software project. The CI process is comprised of automatic tools that assert the new code’s correctness before integration.

Continuous Integration has been adopted widely nowadays. Continuous Integration in GitHub can be done in collaboration with Travis CI. Bitbucket has made CI easily possible by using bitbucket pipelines.

This article teaches about Bitbucket, if you don’t have an account with it, you can go ahead create one.

In your project root directory, create a bitbucket-pipelines.yml file and paste the code below.

Line 1: Defines the python version that you would like to run your project Line 3: Define your pipeline Line 6: caches the pip command providing faster builds, and reducing the number of consumed build minutes. Line 9: Install all your dependencies in the requirements Line 10: Run the test command

Finally, after creating a remote repository on bitbucket and pushing the code, one last step is remaining that enabling Pipelines to kick off your CI/CD pipeline.

It will take some time to install the dependencies and run the command. Next time you are pushing the code, the pipeline will be run and the tests will be carried out.

This methodology is very effective. Assuming you are working on a big project where you have about 5 team members, the automatic tester doesn’t have to wait for all of you to finish the project so that he writes the test cases. In concurrency, he can be writing the test cases as the developers code. When the team pushes code using the git VCS, the tests are run and the developers can see the failed test cases and work on them pronto.

The code for this application can be found here. Till then, try this approach of TDD in order to rise your ranks as a senior developer.

If you like this post, HIT Buy me a coffee! Thanks for reading. 😊

Your every small contribution will encourage me to create more content like this.