Hi all, long time no see! I’m sorry for taking the hiatus too long. In the end, I only use this blog to post my assignments. There’s so much stuff that happened this year, maybe I’ll write about it later if I still have the energy. I hope that you are reading this in a good and healthy condition. I just want to tell you how I implemented TDD in one of my assignments for the Quality Assurance course.
What is the assignment that I got?
In the Quality Assurance course for this semester, I got a project called DIGIPUS; a system application that can accommodate archiving educational material from regional apparatus, academics, and practitioners, and organizing it properly so that it becomes material for increasing knowledge and skills for the community at large. This project is one of the projects from the Software Project course in the last semester, developed by the Marjinal Team. As a member of the Quality Assurance course, I have to implement new features and improve the quality of the code.
Before I started to implement the new feature, I had to prepare the environment of the project. This project used Django Framework (Python 3 + Django 3.1) for the front-end and back-end development and used PostgreSQL for the databases. I created a new Virtual Environment for this project and installed all the requirements. For your information, I used Visual Studio Code in Windows with WSL 2 (Ubuntu 20) for the development. In this first sprint, I implemented the Material: Like/Dislike on a Comment feature.
How did I implement the feature with TDD?
I created some unit tests that contain the abstract implementation of the Material: Like/Dislike on a Comment feature from the model, the views, and the templates. Because each comment is dependent on a material, I created the test in the class DetailMateriTest
. This test class has a setUp()
function that prepared the dummy login credentials and created the dummy material. Here is an example of the unit tests:
def test_comment_disliked_by_anonymous(self):
url_materi = self.url
self.client.get("/logout/")
self.client.login(**self.anonymous_credential)
self.client.post(url_materi, {"comment": "This is new comment by Anonymous"})
comment = Comment.objects.get(comment="This is new comment by Anonymous").id
response = self.client.get(url_materi)
session_id = response.context["session_id"]
payload = {"comment": comment, "session_id": session_id}
ajax_response = self.client.post("/comment/dislike/", payload)
num_of_comment_dislikes = DislikeComment.objects.filter(comment=comment).count()
self.assertEqual(num_of_comment_dislikes, 1)
In each test case, I created a new comment using anonymous credentials and checked if the user liked/disliked that comment then the LikeComment
or DislikeComment
object would be created. I ran the tests and saw that the test failed, this phase is called the [RED]
phase.
After that I implemented the LikeComment
and DislikeComment
models, the views, and the templates that handle the like/dislike buttons. Here is the look of the feature:
For each implementation I run the tests until all the tests are passed, this phase is called the [GREEN]
phase. That’s all that I did to implement the feature with TDD.