Excercise 06: Đại Học Quốc Gia Thành Phố Hồ Chí Minh Trường Đại Học Khoa Học Tự Nhiên Khoa Công Nghệ Thông Tin
Excercise 06: Đại Học Quốc Gia Thành Phố Hồ Chí Minh Trường Đại Học Khoa Học Tự Nhiên Khoa Công Nghệ Thông Tin
EXCERCISE 06
1
Mục lục
2
1 What is Test driven development?
- Test driven development (TDD) is a software development methodology in which tests are writ-
ten before the actual code is implemented.
- The process follows a specific cycle aimed at improving the quality and functionality of the soft-
ware while ensuring that it meets the required specifications.
- In simple terms, test cases for each functionality are created and tested first and if the test fails
then the new code is written in order to pass the test and making code simple and bug-free.
- TDD framework instructs developers to write new code only if an automated test has failed. This
avoids duplication of code (as we write a small amount of code at a time in order to pass tests,tests
are nothing but requirement conditions that we need to test to fulfill them).
- The TDD full form is Test-driven development.
- Test-Driven development is a process of developing and running automated test before actual
development of the application. Hence, TDD sometimes also called as Test First Development.
3
2 Principles of TDD
• Write the codes first: Before writing the actual code, we write a test that defines a new
function or improvement. This test is based on the specifications and requirements of the
feature.
4
3 Workflow of TDD
• 1. Add a test.
• 5. Repeat.
5
6
3.1 Add a test
3.2 Run all tests and see if any new test fails.
- Execute the test suite to ensure the new test fails. This step confirms that the test is valid and
the required functionality is not yet present.
- Write the minimal amount of code required to make the test pass. The focus is on passing the
test, not on perfect code.
- Run the test suite again. The new test should pass if the functionality is correctly implemented.
- Refactor the code to improve its structure, readability, and performance without changing its
external behavior. Ensure all tests still pass after refactoring.
3.5 Repeat
7
4 Example of TDD
4.2 Run all tests and see if any new test fails
- Run the tests to see them fail because the FontSizeSelector component does not exist yet.
- Expected output
8
4.3 Write some code
9
4.4 Run tests and Refactor code.
- It passed.
- In this simple case, our code is straightforward and there’s not much to refactor.
4.5 Repeat
- Repeat the above process like adding more functionalities and test it again.
10
5 Pros and Cons of TDD
- Advantages:
• Better design
- Modular and Decoupled Code: TDD encourages writing smaller, modular pieces of code.
This modularity makes the code easier to understand and maintain.
- Focus on Requirements: Since tests are written based on requirements, the code is naturally
aligned with user needs and business requirements.
• Documentation
- Living Documentation: Tests serve as up-to-date documentation of the code. They show
how the code is supposed to work, making it easier for new developers to understand the
project.
- Behavior Specification: Tests specify the behavior of the code, providing clear examples of
how the code is supposed to be used.
11
- Regression Testing: Automated tests can be run every time the code is changed, ensuring
that new changes do not break existing functionality.
• Enhanced Collaboration:
- Clear Communication: Tests clearly define what the code is supposed to do, improving
communication among team members and with stakeholders.
- Improved Code Reviews: Tests make code reviews more effective by providing a clear
specification of what the code is supposed to accomplish.
- Disadvantages:
• Initial Overhead:
- Time-Consuming: Writing tests before writing the actual code can slow down the initial de-
velopment process. This is particularly challenging for tight deadlines and fast-paced projects.
- Learning Curve: TDD requires a shift in mindset and practices. Developers need to learn
how to write effective tests and follow the TDD process, which can take time.
• Maintaining Tests:
- Test Maintenance: As the code evolves, tests need to be updated and maintained. This can
add to the overall development effort, especially if the tests are not well-structured or if the
code changes frequently.
- False Sense of Security: Passing tests do not guarantee that the software is bug-free. Poorly
written tests or incomplete test coverage can lead to a false sense of security.
12
scenarios.
13
6 Frameworks for TDD
• csUnit and NUnit are open source unit testing frameworks for .NET projects.
• Junit: Widely used unit testing tool for Java TestNG: Another popular Java testing frame-
work. This framework overcomes the limitations of Junit.
14
6.1 TDD vs Traditional Testing
- TDD approach is primarily a specification technique. It ensures that source code is thoroughly
tested at confirmatory level.
- With traditional testing, a successful test finds one or more defects. It is same as TDD. When a
test fails, you have made progress because you know that you need to resolve the problem.
- TDD ensures that your system actually meets requirements defined for it. It helps to build your
confidence about your system.
- In TDD more focus is on production code that verifies whether testing will work properly. In tra-
ditional testing, more focus is on test case design. Whether the test will show the proper/improper
execution of the application in order to fulfill requirements.
- In TDD, you achieve 100% coverage test. Every single line of code is tested, unlike traditional
testing.
15
7 References
1. Thomas Hamilton, & Hamilton, T. (2024a, April 30). What is Test Driven Development
(TDD)? example. Guru99. https://www.guru99.com/test-driven-development.html
2. What is Test Driven Development (TDD) ?. BrowserStack. (2023, June 22). https://www.
browserstack.com/guide/what-is-test-driven-development
16