Include 3 parts in each test name

One Paragraph Explainer

A test report should tell whether the current application revision satisfies the requirements for the people who are not necessarily familiar with the code: the tester, the DevOps engineer who is deploying and the future you two years from now. This can be achieved best if the tests speak at the requirements level and include 3 parts:

(1) What is being tested? For example, the ProductsService.addNewProduct method

(2) Under what circumstances and scenario? For example, no price is passed to the method

(3) What is the expected result? For example, the new product is not approved

Code example: a test name that incluces 3 parts

  1. //1. unit under test
  2. describe('Products Service', () => {
  3. describe('Add new product', () => {
  4. //2. scenario and 3. expectation
  5. it('When no price is specified, then the product status is pending approval', () => {
  6. const newProduct = new ProductService().add(...);
  7. expect(newProduct.status).to.equal('pendingApproval');
  8. });
  9. });
  10. });

Code Example – Anti Pattern: one must read the entire test code to understand the intent

  1. describe('Products Service', () => {
  2. describe('Add new product', () => {
  3. it('Should return the right status', () => {
  4. //hmm, what is this test checking? what are the scenario and expectation?
  5. const newProduct = new ProductService().add(...);
  6. expect(newProduct.status).to.equal('pendingApproval');
  7. });
  8. });
  9. });

“Doing It Right Example: The test report resembles the requirements document”

From the blog “30 Node.js testing best practices” by Yoni Goldberg

A test report example