TDD with Cyber-Dojo

TDD with Cyber-Dojo

·

4 min read

Hello World!

TDD which is the abbreviation of Test Driven Development is a software development process, that relies on the Code and Test Case. It has a sequence

  • The tester writes a test case.

  • The developer tries to write code, that passes the test case

  • The tester tries to write a valid test case, which makes the code fail.

  • The developer rewrites the code so that it passes all the test cases

  • The tester includes a new test case that makes the code fail

This cycle continues until the code passes all the test cases written by the tester. In the end, we would get a code with better quality. This is Test Driven Development.

Cyber-Dojo

Cyber-Dojo is a tool that is used to practice TDD. It is a shared learning environment. We can practice TDD in Cyber-Dojo.

Home page of cyber-dojo.org

How to use CyberDojo?

To create a new problem

  1. Click on set up a new practice session.

  2. Select the language in which you code and the test framework and click next.

Selecting language (Python) and test framework (py.test)

3. There will be a set of available problems. If you want to create a new problem statement, click verbal in the list and click Ok.

To create our own problem, click on verbal

4. Now a session id is generated. Make a note of that session id and click ok.

A session code is created

5. Now click on Enter a practice session and type the session id and click on start.

Entering a practice session

6. Now an avatar (here the avatar will be animals) is generated for you. Make a note of this avatar. The avatar is used to identify the user. Whenever a user starts a session using a session id, an avatar is generated for the user.

A Tiger avatar is generated for the session

The user can resume the practice later by selecting the avatar in the Continue option.

Continue option avatar selection

There is a total of 64 avatars, which means 64 sessions can be created using a session id.

7. Now the session will start in a new tab. It contains,

  • hiker.py (in the case of python) — where we write code

  • instructions — where the instruction is given by the instructor.

  • test_hiker.py — where we write test cases

  • output — where we could see the output

  • cyber-dojo.sh — It is a shell script where the logic of TDD is written by the creator.

New Session

It is advisable to rename the hiker.py and test_hiker.py according to the problem that we are working on.

For example, if I am working on a problem called Binary Range, then I will rename hiker.py -> BinaryRange.py and test_hiker.py -> test_BinaryRange.py.

It is also advised to change the name of methods and classes so that the program looks meaningful.

Renamed files and methods, classes

8. Now click on the test button.

You could see some number generated near the avatar and something like a red light.

The number is nothing but the version of the code.

The ‘Red Light’ is nothing but a traffic light, which tells you whether all the test cases are passed, any test case failed, or a compiler error.

There are 3 color codes for traffic lights

  • Green — All test cases passed

  • Red — At least one test case failed

  • Yellow — Compiler Error

Clicking the test button would show the output file. In our case, we have one test case and that one test case failed. So the red light will glow on our timeline.

Result 1 — test failed

Now I have modified the code so that it passes the test case. Now green light glows as all test cases are passed.

Result 2 — test passed

If I have made any compiler error, then the yellow light will glow.

Result 3 — Compiler Error

The ‘RED’ and ‘GREEN’ combination continues and the final code will be of better quality.

The Dashboard will show the Red — Green — Yellow pattern for each avatar.

We can also see the differences in each iteration in diff. And we can create a new session from the current version of the problem by clicking Create a practice session button.

That's all about the Cyber-Dojo. Hope this is helpful to you.

Looking forward to your valuable feedback and suggestions.

Thank you!!