What is TDD Add a little test Run all tests and fail Make a little change Run the tests and succeed Refactor to remove duplication From Kent Beck Author of “Test Driven Development By Example” and xUnit
TDD TDD là cái gì?
Unit Test
Good Programer Test Minimize programmer waiting. Run reliably. Predict deployability. Respond to behavior changes. Not respond to structure changes. Be cheap to write. Be cheap to read. Be cheap to change
Unit test value proposition Has a high chance of catching a regression bug. Has a low chance of producing a false positive. Provides fast feedback. From Vladimir Khorikov
Style Of Unit Test Functional style
Style Of Unit Test State verification
Style Of Unit Test Collaboration v erification
SOU examples
When to use Collaboration Verification Style? Just 1 Collaboration Test verify the order, times method invoked, and the parameter pass to method Many function style test to verify output/state of sut
What should be unit tested? Common code used in many modules Code that frequently changed Code that frequently causes bugs
What shouldn’t be unit tested? External libraries or frameworks Trivial code (such as getter/setter) Code that works only with UI Code that only access Database Private methods
Smelled Unit Test It calls Database It communicate through networks It read the file system It can’t run parallel with others It depend on environment variable It test private method It test method works only with UI It need complicated setup with a lot of mocking It complicated compare with code (sometime event more)
Code coverage "In computer science the Pareto principle can be applied to optimization efforts. [17] For example, Microsoft noted that by fixing the top 20% of the most-reported bugs, 80% of the related errors and crashes in a given system would be eliminated. [18] Lowell Arthur expressed that "20% of the code has 80% of the errors. Find them, fix them!" [19] It was also discovered that in general the 80% of a certain piece of software can be written in 20% of the total allocated time. Conversely, the hardest 20% of the code takes 80% of the time. This factor is usually a part of COCOMO estimating for software coding. WordPerfect and other software developers identify what customers want most of the time and how they want to do it: the 80/20 rule (people use 20% of a program’s functions 80% of the time). Software developers work to make high-use functions as simple and automatic and inevitable as possible" Code coverage should be used as a guidance to where we need more test than a standard or KPI
Refactoring Refactoring is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior. From Martin Fowler => Refactor is not optimization ? => Refactor is not extension but refactoring to make extension easier
Refactoring knowledges Refactoring Improving the Design of Existing Code Refactoring Technique Design Pattern SOLID principles .
Refactoring tool Manually rename a function that used 200 times throughout the code base? Tool encourages developers refactoring Resharper is the best but costly (300$/dev/year) Roslynator is free one that closest to Resharper (but still very far behind)
People’s racket of refactoring: it needs more time Code access limitation Git Branching Model Not enough test Legacy code Database Refactoring's Obstacles
Refactoring is an investment to improve the internal quality of code that Make it easier and faster to do the current work Make it easier and faster to modify / extend in the future Developer freely do refactor every day to help him do the current task faster, easier - Make change easy then make easy change (Kent Beck) Developer talk to stakeholders for refactor that invest for the future to get approval If you see refactor make you work slower , stop it. If something is messy but working, let it that way, don’t refactor unless you need to update for business Refactoring practice
TDD with Legacy code Legacy code? The main thing that distinguishes legacy code from non-legacy code is a lack of comprehensive tests From Micheal Feather Author of Working effectively with the legacy code TDD with Legacy code = Test Driven Refactoring
Top-down
Bottom-up
Find Something that should be refactored
Extract Method
Refactor to pure function
Courses Refactoring course Unit Test Course TDD course And more on Pluralsight, Udemy...
References https://enterprisecraftsmanship.com/ Programer Test Principles https://martinfowler.com/bliki/UnitTest.html Test Driven Development By Example Refactoring Improving the Design of existing code Working Effectively with the legacy code