Exploring Testing Code

Enhancing Code Reliability and Quality Through Testing

What is testing?

Testing ensures that the code you've written works as intended. Imagine you're cooking. After completing each step, you taste your food and realize it's not good enough to eat. However, if you test as you cook, you can fix any mistakes before it's too late. Testing in code is just like that. It's a way to check software or applications before they're fully made, so you can spot and fix any problems early on.

Why we need testing?

Testing is super important in software engineering. Imagine you're building an app. After writing lots of code, you add a new feature that changes some old code. But when you try it out, the app breaks! Now, you have to test the whole app to make sure everything else still works. This is called manual testing, and it takes a lot of time. But if you write automated test cases for each part of your app, you don't need to test manually every time. With a single command, you can test the whole app! Automated testing takes extra time upfront, but it saves a lot of times compared to manual testing. Plus, it gives us consistency and reliability, making us more confident there are fewer errors in our app.

Types of testing

There are three types of testing.

  1. Unit Testing: This involves testing individual units or components of the software independently. It focuses on verifying that each unit functions correctly as expected..

  2. Integration Testing: Integration testing checks if different units or modules of the software work together seamlessly when integrated. It ensures that interactions between various components do not cause unexpected behavior.

  3. End-to-end Testing: End-to-end testing assesses the complete functionality and workflow of an application, from start to finish, ensuring seamless performance across all components and integrations in a real-world environment like a real person.

Test Example

function multiply(a, b) {
    return a * b;
}

const multiplyResult = multiply(5, 2);

// Write our test, we know 5 * 2 = 10
if (multiplyResult !== 10) {
    throw new Error("Multiply with the argument 5 and 2 should return 10");
}

In this example, we're testing a function called multiply. We want this function to always give us 10 when we give it 5 and 2 as arguments. We're writing this test manually. But, there are many testing libraries for JavaScript like Jest, Mocha, Enzyme, Jasmine, etc., that make testing easier. The other languages definitely have library for testing.

If we use Jest to write the same test for our multiply function, here's how it would look:

test("multiply 5 * 2 should be 10", () => {
    expect(multiply(5, 2)).toBe(10);
});

Test Driven Development (TDD)

Test-driven development is a way of writing code where you first write tests to check what your code should do. Then, you write the actual code to make those tests pass. This helps make sure your code does what it's supposed to and stays working even if you change it later. It also helps you think about potential problems early on and makes your code easier to understand and update.