0% found this document useful (0 votes)
27 views16 pages

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

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views16 pages

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

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

ĐẠ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

GIẢNG VIÊN HƯỚNG DẪN:

Thầy Hồ Tuấn Thanh

Thông tin sinh viên:

21120297 - Phùng Lê Hoàng Ngọc

THIẾT KẾ PHẦN MỀM - 23_24


Hồ Chí Minh, ngày 18 tháng 4 năm 2024

1
Mục lục

1 What is Test driven development? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3


2 Principles of TDD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3 Workflow of TDD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
3.1 Add a test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.2 Run all tests and see if any new test fails. . . . . . . . . . . . . . . . . . . . 7
3.3 Write some code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.4 Run tests and Refactor code. . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.5 Repeat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
4 Example of TDD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4.1 Add a test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4.2 Run all tests and see if any new test fails . . . . . . . . . . . . . . . . . . . . 8
4.3 Write some code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4.4 Run tests and Refactor code. . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4.5 Repeat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
5 Pros and Cons of TDD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
6 Frameworks for TDD . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
6.1 TDD vs Traditional Testing . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
7 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

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.

• Incremental Development: Development is done in small, manageable increments. Each


new feature or improvement starts with a failing test.

• Red, Green, Refactor: This is the core cycle of TDD:


- Red: Write a test and see it fail.
- Green: Write just enough code to pass the test.
- Refactor: Improve the code while ensuring the tests still pass.

4
3 Workflow of TDD

- Following steps define how to perform TDD test

• 1. Add a test.

• 2. Run all tests and see if any new test fails.

• 3. Write some code.

• 4. Run tests and Refactor code.

• 5. Repeat.

5
6
3.1 Add a test

- Start with a specific requirement or user story.


- Write a unit test that will validate this requirement. This test should initially fail since the func-
tionality is not yet implemented.

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.

3.3 Write some code

- Write the minimal amount of code required to make the test pass. The focus is on passing the
test, not on perfect code.

3.4 Run tests and Refactor 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

- Continue this cycle for each new feature or piece of functionality.

7
4 Example of TDD

- An example of Test Driven Development used in Front end (React JS)


- User story: User can choose text font size of the novel they’re reading.

4.1 Add a test

- Create a new test file FontSizeSelector.test.js in the src directory.

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

- Create the FontSizeSelector component in src/FontSizeSelector.js.

9
4.4 Run tests and Refactor code.

- Now run the test again to check if it passes

- 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:

• Improve code quality


- Clarity and Precision: Writing tests first clarifies the requirements and leads to more precise
and thoughtful code. It forces developers to consider the edge cases and how the code should
behave.
- Fewer Bugs: As tests are written before the code, many potential bugs are caught early in
the development process, reducing the overall number of defects.
- Refactoring Confidence: With a suite of tests, developers can refactor code without fear of
introducing new bugs. The tests ensure that the functionality remains consistent.

• 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.

• Shorter Debugging Time


- Immediate Feedback: Running tests frequently gives immediate feedback on whether the
code works as expected, reducing the time spent on debugging.

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.

• Complexity in Testing Certain Features


- UI and Integration Testing: Writing tests for user interfaces, asynchronous operations, and
integration points can be complex and challenging. Specialized tools and techniques are often
required.
- Mocking and Stubbing: Extensive use of mocks and stubs can make tests more complicated
and harder to maintain. It can also lead to tests that do not accurately represent real-world

12
scenarios.

• Shorter Debugging Time


- Immediate Feedback: Running tests frequently gives immediate feedback on whether the
code works as expected, reducing the time spent on debugging.
- Regression Testing: Automated tests can be run every time the code is changed, ensuring
that new changes do not break existing functionality.

• Not Suitable for All Projects


- Prototype and Experimental Projects: For projects where requirements are not well-defined
or are expected to change rapidly, TDD might not be the best approach. Rapid prototyping
and iterative development might be more suitable.
- Legacy Code: Applying TDD to legacy code that was not originally designed with testing
in mind can be difficult and may require significant refactoring.

13
6 Frameworks for TDD

• csUnit and NUnit are open source unit testing frameworks for .NET projects.

• PyUnit and DocTest: Popular Unit testing framework for Python.

• Junit: Widely used unit testing tool for Java TestNG: Another popular Java testing frame-
work. This framework overcomes the limitations of Junit.

• Rspec: A testing framework for Ruby projects

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

You might also like