How To Write Unit Tests (The Right Way)

preview_player
Показать описание
In this video I will teach you how to write better unit tests in under 15 minutes. Throughout my career I've had the opportunity to teach other software engineers about effective unit testing. Honestly, these skills are not too difficult to master once you understand what they are. In this video we will discuss what unit tests are, why you should only test your code's behavior, how to simplify your unit test setup, why one assertion per test is enough, and why it's important to keep your unit tests isolated from one another. Wow, what a run-on sentence that was. While we won't go over how to do test driven development in this video, this is a really good prerequisite for that.

🚶‍♂️ FOLLOW ME 🚶‍♂️

⏰ TIMESTAMPS ⏰
0:00 - Intro
1:24 - What Are Unit Tests
2:02 - Test The Code's Behavior
3:30 - Simplify Unit Test Setup Process
7:36 - One Assertion Per Unit Test
10:00 - Keep Unit Tests Isolated

🎥 CAMERA GEAR 🎥
* Note: the above links are affiliate links.

🖥 DESK GEAR 🖥
* Note: the above links are affiliate links.

#unittesting #softwareengineer #softwaredeveloper
Рекомендации по теме
Комментарии
Автор

What are your favorite tips for writing good unit tests?

CodyEngelCodes
Автор

Would love to see you teach a beginner tutorial on unit testing. It would be really awesome to learn unit testing from someone with such vast industry experience.

DedicatedManagers
Автор

test_like_button ✅ *[PASSED]* (1m21.69s)

LyricWulf
Автор

I'd like to understand the difference among types of test doubles: mocks, stubs, fakes, dummies and so on.
Many books waste many pages to try explaining the subtle differences among them, but without success... imho

giuseppemodugno
Автор

Tests with random data generation are not reproducible. Not a very Staff thing to do.

linkinparkonstage
Автор

Wow ... really useful, thank you so much for sharing. Also, subscribed :)

levon
Автор

package smash.that.like.button had me dying laughing.

brandonw.
Автор

Can you give actual examples with code next time? This was great! Thanks!!!

dansteryoo
Автор

Check out jquick (java), fast-check (js), hypothesis (python)... best property-based testing libs with support for arbitraries (which were called "factories" in the video)

segevmalool
Автор

I’ve had that same experience of having limited understanding meaning that there’s a communication issue

fluffyfetlocks
Автор

How about for a string parameter testing blank, a normal size string and a string that is too long. I assume these would be 3 different unit tests, correct?

PatrickSteil
Автор

How do you write unit tests for a long class method having 1500+ lines of code? Also it has lots of global dependencies (in a form of class static methods). The test case I set up has 100+ lines just for mocking so as to get to the line of code I am asserting. OMG

fredlyw
Автор

wish you had code examples for everything you pointed out, like the factories.

wjrasmussen
Автор

If you used his idea of random number/string generators, make absolute certain to include the generated string in your messaging. Otherwise, tests will sporadically fail and you will have to suss out the input that made them fail.

SaintSaint
Автор

I don't think using "random" is the best idea. Perhaps writing "fake" string, but one that doesn't change from run to run would be better.

danielwilkowski
Автор

Great content . Thanks a ton and much respect for your efforts here. Could you please share how you evaluate new software libraries? how to evaluate Android libraries to see how they fit the needs of an Android application. What kind of metrics do you generate and how ? If you could walk through with an example( like evaluating Room library or Hilt ).Specifically, How do you evaluate a software library for scalability. What tools or tests help? Thanks again.

nalinitippa
Автор

test_like_button ❌ *[Failed]* --> it turned black :D

mystartupjourney
Автор

I am a teacher and I've been trying to write tests for my students. However, there are not any good tutorials for testing the main method of a class. When I am teaching constants, I want to test that they are setting specific variable to a constant, and using the convention of capitalizing a constant variable. Would you be able to help with this?

christopherwilson
Автор

You need to expand your definition of unit test, imo.
A unit test needs to be standalone runnable from the command line.
A unit test needs to be executed as part of your build pipeline.

If those two things aren't true, you aren't really unit testing, you're just testing imo.

Those two things impose a third restriction on unit tests.
They need to be fast.

khatdubell
Автор

Hi Cody can you recommend C# unit tests 'how to'?

preneshreeperumal