DEV Community

Cover image for Clean Code - How to write proper test case names
Bharathwaaj S
Bharathwaaj S

Posted on

1 2

Clean Code - How to write proper test case names

A test case consists of two parts

  1. Action
  2. Result

Test cases should then be named accordingly. For an action, it should produce a result. The action word should be modified as per the context.

def test_action_produces_result

In the above name, the produces is the action word.

Check these names (Replace model with your specific table name)

def test_form_saves_title_field_in_model
def test_post_redirects_to_home_page
def test_recalculate_updates_score_field_in_model

Corner cases can be tricky. You might be tempted to write otherwise. For ex:

def test_score_field_is_present_in_excel

In this case the action gets missed. However, the formula of action_produces_result can be always applied to provide more context

def test_exported_excel_has_score_field

There are some cases where you aren't bothered about the result. These are usually in cases of regression. For ex:

def test_users()

However too many test cases of this type are mostly a result of laziness and should be cleaned up.

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Generate and update README files, create data-flow diagrams, and keep your project fully documented. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE