0% found this document useful (0 votes)
4 views

testing_notes

Uploaded by

28409
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

testing_notes

Uploaded by

28409
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1.

Testing

Testing is about checking if your code works as expected. Think of it like checking
if a recipe you followed actually results in the dish you're expecting. In
programming, we write tests to make sure our code behaves correctly.

There are different types of tests:

• Unit tests: Test individual pieces (functions or methods) of your code.

• Integration tests: Check if different pieces of your code work together.

• End-to-end tests: Test the entire application, often simulating how a real
user interacts with it.

2. Mocks, Stubs, and Fakes

These are ways to simulate parts of your code or external systems (like a
database or API) while testing, so you don't always have to use the real thing.

• Stub: A basic fake version of a function or object. It gives a predefined


answer when called, useful for isolating your test.

o Example: You might have a function that calls an external API, but
during testing, you don’t want to call the real API. A stub can return
a fake result instead.

• Mock: A more advanced fake object that not only gives predefined
answers, but also keeps track of how it's used. You can check if a
function was called, how many times, and with what parameters.

o Example: If you're testing a function that sends an email, you might


use a mock to ensure it tries to send the email with the right
content.

• Fake: A fake object or service that has a working implementation, but it’s
simplified or in-memory (e.g., a fake database that stores data in memory
instead of a real database).

o Example: A fake database could store data in memory, so your


code can interact with it as if it were real, but it's much faster for
testing.

3. Test-Driven Development (TDD)

TDD is a way of writing code where you write tests before writing the actual
code. The process goes like this:

1. Write a test for a small piece of functionality you want to add.


2. Run the test (it will fail because you haven’t written the code yet).

3. Write the code to make the test pass.

4. Refactor your code to make it cleaner, while keeping the test passing.

5. Repeat this for each small piece of functionality.

TDD helps you focus on writing code that meets the requirements and ensures
that your code works as expected.

Putting it all together:

• Testing is about verifying your code works.

• Mocks, Stubs, and Fakes help simulate parts of the code or external
systems during testing.

• TDD is a methodology where you write tests before writing the code,
helping you ensure your code meets the requirements and works
correctly.

You might also like