005 What is TDD

005 What is TDD

How to Write Tests in an Application

Introduction to Testing

  • The lecture focuses on completing tests within an application, encouraging viewers to attempt writing tests independently before demonstrating the process.

Creating a Tax Object

  • A new tax object is created with a net income calculation using the formula: net income = gross income - tax amount.
  • An example calculation shows that for a gross income of 200 and a tax rate of 20%, the expected net income is 160. The test passes successfully.

Running Multiple Tests

  • Both tests can be executed simultaneously, allowing for efficient testing. If one fails, details are provided for debugging.
  • The instructor notes inefficiency in initializing the same tax object multiple times without passing arguments.

Using Before and After Functions

  • In larger projects, it's essential to initialize objects efficiently. A before function allows setup before running tests.
  • The syntax for the before function is flexible; it must be annotated correctly but can have any name.
  • An after function (often called teardown) executes after each test ends, which will be useful in larger projects.

Basic Testing Principles

  • Tests should show green text if they pass and provide feedback if they fail, guiding developers back to their code for corrections.

Understanding Test Driven Development (TDD)

Introduction to TDD

  • TDD stands for Test Driven Development, emphasizing development around testing principles rather than traditional methods.

Continuous Integration Cycle

  • TDD promotes continuous integration where testing and development occur iteratively rather than sequentially. This approach helps identify issues early in large projects.

Writing Tests First

  • TDD suggests writing tests before developing functions to avoid bias towards making functions pass existing tests. This method encourages better design practices from the outset.

Benefits of TDD in Large Projects

  • In extensive projects lasting several months, immediate feedback through testing prevents discovering significant issues long after coding has been completed.

Understanding Test-Driven Development (TDD)

Introduction to TDD Concepts

  • The speaker emphasizes the importance of thinking about tests before writing functions, especially in real-life scenarios involving fragments, view models, and repositories.
  • It is noted that while one might write tests for a view model before completing it, this approach can complicate the learning process for beginners.

Key Principles of TDD

  • A critical aspect of TDD is having only one assertion per test. Although multiple assertions may sometimes be unavoidable, it's recommended to minimize them.
  • If two assertions are present in a test, splitting the test into two separate tests is advised to adhere to TDD principles.

Integration of Testing in Development Cycle

  • TDD integrates testing into the core development cycle; developers should conduct tests prior to proceeding with further development.