DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

[DZone Research] Observability + Performance: We want to hear your experience and insights. Join us for our annual survey (enter to win $$).

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Advancements in Mobile App Testing: Harnessing AI's Potential
  • Test Automation Success With Measurable Metrics
  • Top Five AI-Powered Tools for Test Automation
  • End-To-End Automated Testing of a Home Robot Using April Tags

Trending

  • Multi-Tenancy With Keycloak, Angular, and SpringBoot
  • LLMs for Bad Content Detection: Pros and Cons
  • AI for Web Devs: Project Introduction and Setup
  • Auto-Scaling DynamoDB Streams Applications on Kubernetes
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. A List of Best Practices for Writing Tests

A List of Best Practices for Writing Tests

While writing tests, these guidelines will serve as the best practices to follow, which can lead to better-quality tests.

Harshvardhan Singh user avatar by
Harshvardhan Singh
·
Jun. 30, 23 · Opinion
Like (1)
Save
Tweet
Share
2.93K Views

Join the DZone community and get the full member experience.

Join For Free

Below is a list of best practices to follow when writing tests. These are general guidelines one should follow irrespective of the type of tests they are working on.

Keep Tests Independent

Each test should be independent of others, ensuring that a failure in one test doesn't impact the execution or outcome of another. This improves test reliability, enables test parallelization, and allows for easier debugging.

Don't share the state between tests. Set up the state before the execution of the test and tear it down after it executes. This will keep tests isolated.

Write Tests Early

Start writing tests as early as possible in the development process. Early tests help identify issues sooner and provide feedback early in the development lifecycle. As we move towards the later stages of the development lifecycle, the cost of fixing the issue increases. For example, finding and fixing an issue during local development is easy and cheap, but if the same issue is moved to production, then it wouldn't be easy to find the issue, and it may potentially cost the organization to lose money, users, or trust.

Test Behavior, Not Implementation

Test application through the public behavior exposed by the application. For example, for a Web API, if tests are invoking Controller's method, then these tests are testing the implementation, not the behavior, because this is not the public behavior exposed by the Web API. The public behavior is HTTP API, and tests should be written at that level and not any layer below it. This keeps your tests decoupled from implementation, and this gives you the ability to change the implementation however you like as long as your public behavior is consistent.

Manage Complex State Through Builders

Often to test a scenario, we need first to build a state. And this sometimes requires quite a bit amount of code to set up. Manage and simplify this state created through the use of Design Patterns such as Builders.

Write Focused Tests

A test should verify only one thing. It should not verify multiple statements one after the other. Keeping tests small and focused will help in maintaining the tests as well as when it fails, you will know exactly what failed. But if a test contains multiple verification steps, then you would have to log in to verify what exactly has failed in the test.

Use Multiple Levels

Have a test suite with multiple levels. For example, unit tests, integration tests, and end-to-end tests. And then, when you want to write a test, think at which level it could be tested better and then write the test at that level. For example, testing a public library method could be done in unit tests, whereas a database interaction could be done in integration tests.

Testing Test automation

Opinions expressed by DZone contributors are their own.

Related

  • Advancements in Mobile App Testing: Harnessing AI's Potential
  • Test Automation Success With Measurable Metrics
  • Top Five AI-Powered Tools for Test Automation
  • End-To-End Automated Testing of a Home Robot Using April Tags

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: