Making Content Randomizer and Test it

So in late September 2020, I got a task to contribute new features that possible to add to a specific web application project. A little bit background, the main purpose of this web is to delivering educational content from any contributors to people through the public web, so everyone can register to the web as a contributor and make any educational content as they want, and also people don’t need to register to the page if they just want to see the contents. Of course, the web has built with an approval system, so the content will be delivered to the home page as long they meet the qualifications.

A Brief Things to Say before Start

Because this is an open-source and also there is a development before me, so first I need to learn how they coded, learn the style they used, learn the file structure, and did the first setup. The file structure is the hardest part for me, but I managed to get it all done in 10–30 minutes because I am already familiar with that kind of environment. Not all of them but at least at that time period, I knew some parts and what I can and should do in that project.

For developers, just to know what kind of project I worked with, these are the characteristics the project have:

  • Django for back-end and front-end.
  • PostgreSQL as the main database
  • GitHub Flow-like (There is no staging, just development and master)
  • The pipeline contains tests, lint check, deploy to a dynamic analysis tool, and deploy to Heroku (Production Server)

The content randomizer has been implemented by following the TDD cycles, so I begin it by creating a test first, continue with the implementation, and ensure there is no failure task. You realized that I don’t set up a skeleton of code along with the test, it’s because I only insert some codes to an existing function for the functionality I will make.

Test Unpredicted Result

So before I jump to test, I need to think which one from the functionality that is predictable and what is not. Let’s take a look at what I got.


  • The content of the randomized list has the same contents as original


Anything else? This is the best I got, just comment if you found another. As you can see there is something we can do even though it’s random. So here is the test code for predictable things.

Test status code and the contents

So in setup, I actually only create two contents so that’s why I only check two items there.

How about the unpredictable things that I just mentioned? Actually, there is a way to test that. We know that the purpose of this functionality is showing each content in any position, so I came up with an idea that we need to run the same test twice and check those two lists that must have the different content orders. Another problem arises, how if these two lists get a chance to have the same order of content? That’s why we need to run these tests again until it successfully passed. But how if it never successfully passed? to overcome this we could set a maximum time to run all same tests or set a maximum number of the calling same tests. Surely, the second option is more preferable, that’s why I choose it.

Here are the codes to test unpredictable things.

Conduct several same tests until it passed with a limit iteration

By setting the number of tests is 10, it’s surely enough will pass the test with a probability is almost 100%. Because normally tests will pass in a first or second iteration.

Making a Random Functionality

The button to trigger content randomizer, and when that happens will refresh the page with new randomized contents
Handle a random content request in the controller


TDD is so interesting to have. Personally, TDD helps me to scope my implementation. Sometimes as a developer, we want to implement far than what requirements want, better than requirements, so the users or may developers will be satisfied with the improvisation of improvements. But since it is driven by test, our implementation will not be going far because the tests are limited.

Mainly a Full-stack Developer, Software Architecture Engineer. I'm a passionate developer, love to learn new things and write an article about it