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.

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay