- [Instructor] So you've been amazingly productive
with vibe coding, but you want to take it to the next level.
Well, you're in the right place as you continue your journey
to be a deliberate developer.
Next up, we're going to look at test driven development,
often rewrite code, and then we think about improving it.
But let's flip that on its head now
and instead think about test driven development or TDD.
And this is where your tests lead the way.
It's built on a simple, intentional, and powerful rhythm,
the red green refactor cycle.
The TDD mantra starts with a paradox.
Your first goal is actually to fail.
You're going to begin by writing a test
for a piece of functionality that doesn't even exist.
That test will, and of course, must fail.
And we call this the red phase.
So let's start with a very simple problem.
We'll create a function that validates a new password,
and we'll have a simple first requirement
that it must be eight characters long.
So here's the code.
As we can see, it's empty.
There's nothing in there yet.
The main.py that will have our password validation
and then the test_main.py that will have our tests.
So let's start by creating our first test.
And that's here, I'm using a library called pyTest,
which you have to pip install.
So I have my first code, my first test for it.
So I'm going to say pytest test_main.py.
And there's a lot of red here.
I'm getting errors as it's doing it. This is good.
This is what I want.
It's proving two things.
Number one, our test code is working,
and number two, our feature is not yet implemented.
So we have a clear,
very specific goal on what we want to do next,
and that's the green phase.
Our mission is to turn the test from red to green
as quickly as possible.
We're not aiming to be elegant, just to pass the test.
So let's go to the main.py file
and let's create a function
with the absolute minimum amount of code
that we want in there.
We're testing validate_password.
So I'm going to implement validate_password.
Remember in the test, we imported validate_password,
and we're inserting validate_password.
So now in main,
I'm just going to create this very basic simple one.
It's not close to a complete function yet.
But what about our test?
So let me save this out.
Let me go back to test.main and let me run it again.
Now the test has passed. We're green.
The code is correctly handling
this way too short a test case,
but we've made a tiny step forward
and our code is working.
And that takes us to the final step, which is refactor.
Now that we have a safety net of a passing test,
we can clean up our code without fear of breaking it
using that test as a validator.
Now this code is very, very simple.
It doesn't really need a refactor,
but that's just one simple test
that it should be more than eight characters
and to fail if it isn't.
So let's try a password that's long enough,
and we'll write a test case for that.
And here's my test case.
Is the password longenough,
and I'm going to validate this password, longenough.
Very secure password, I know.
So let me save that out and let's run the test.
Now it's failing.
We have red again.
What's happening here?
Well, we need to drive it to green
by fixing the main function
to deal with a password like this.
So let's update the code with this logic.
Here, we're going to see if the password is less than eight,
it's false, otherwise we'll return true.
We're adding a passing condition.
And we can go back and rerun the test.
And now it's green. Both tests are passing.
So now it's time to do the refactor setup.
So let's take a look at the code again,
there's something really untidy here.
I've spotted it. Maybe you did.
Now would be a good time if you're using Gemini
to ask Gemini about it.
And this code could be simplified by Gemini
to a single line, something like this.
So now I'm just saying return len(password)
greater or equal to eight.
So instead of returning false, return true,
it's all on one line.
So let's save that out.
Let's run our tests again, and they're still green.
Now this is a super simple example
to show you the idea of red green refactor
in test driven development.
Next up, we're going to explore
how Gemini can be a powerful tool to help us through that,
in particular, the green and the refactor phases.