5 Unit Testing Tips for Refactoring Legacy Code

Опубликовано: 13 Апрель 2026
на канале: Lethcode
827
15

This video has 5 tips to make your day easier when unit testing code that didn't have tests before. Legacy code normally was never designed to be tested but with some easy tweaks you can make it much easier to test your code. The tips aren't specific to any language but there are some examples in .Net using XUnit that hopefully you can follow along with even if you don't use .Net.

We will be using the TicTacToe app from a previous video. Adjusting the code slightly so unit tests are possible. We will point out the structure of a good test, breaking up test cases and testing attributes in Xunit. On top of that we show how you can test internal variables/methods in your unit tests.

TicTacToe in under 200 Lines -    • How to Write Tic Tac Toe in .NET WPF in un...  
Git Repo Link - https://github.com/alethcoe/TicTacToe...
XUnit Theories - https://andrewlock.net/creating-param...

**Get 2 FREE Audio Books from Audible **
https://amzn.to/2ZfViDn

My Favorite Books on Devops and Leadership

The Phoenix Project https://amzn.to/3ahITFm
The Unicorn Project https://amzn.to/2NtIKFD
The Devops Handbook https://amzn.to/3jMG6al
The Infinite Game https://amzn.to/3b9AjHO
Start With Why https://amzn.to/3pf6jPS
Clean Code https://amzn.to/3b2dOEU
Code Complete https://amzn.to/2NtIYMZ

Tips

1. Unit tests give us more confidence and allow us to save time over manual testing.

2. Changing method signatures can help test, overloading allows the program to run as it did.

3. Good Test names help you narrow down any problems that come up when something fails.

4. Don't test every possiblity. Test the most realistic buisness cases and how things may fail.

5. Tests don't need to call the method they are testing directly. They can test inner methods that are private.



Gear Used
Blue Snowball iCE USB Mic https://amzn.to/2ZiPpVT
Intel Core i5-10600KF - 8GB Memory - NVIDIA GeForce GTX 1660 SUPER - 1TB HDD + 240GB SSD
https://amzn.to/2ZeZT91
*******
Davinci Resolve - Editing
StreamLabs OBS - Screen Capture



**Disclaimer: Lethcode is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to www.amazon.com.