-Hello, everyone.
In this video, we will be looking at how to create a test class
and some tests within it.
In the last part, we tried to see if our accessors were working correctly.
Right now, we want to replicate this behavior within a test.
In order to do that, let's just go to the counter class
and create "CounterTest",
that will be a subclass not of objects but of test cases.
And we will not need the "count" instance variable.
Now we can press Cmd+S.
The CounterTest has appeared.
You can see that there is a grey button on the left side.
This grey button can be used to run over tests within the class.
Right now, there are no tests so it is not doing much.
But we will just create one that we will call :
"testCounterisSetAndRead".
Let's just copy and paste the code right here
and on the last line, so the last expression,
we will not simply output the value of an instance variable
but run it against 7 and see if it is correct.
In order to do so,
we can just write "self assert",
then "c count"
and we want to run this against "equals 7".
This will try "c count" and see if it is equal to 7,
which is what would set it up to, so it should work.
Let's press Cmd+S.
Our method is created and we can see that,
the same way there is a grey button at the side of the name of the class,
there is a grey button there,
so this will run the test for this method.
If we click on it, we can see that it ran and it passed.
If we click on this button there,
it will run all tests from within the class.
It will only run one for now.
When your tests are green,
it is a good time to save your image.
We can go to the menu, Pharo, Save.
Now, let's create another test but with another mindset.
Let's try to do some test-driven development.
What that means is that we will write the test first,
and implement the method it needs to be green.
We will just try to make a test increment.
We will set the counter to 4,
then increment twice
so we can do with a semicolon.
"Increment; Increment".
And we want it to be equal to 4 and let's put this one too.
Okay.
This should add 2 to the counter.
Let's compile it.
We can see that the method has appeared.
But if we click on the button there,
it should fail because we did not define the increment method,
so let's click on it.
Yes. "Instance of counter did not understand increment".
That's right, we have to define it, so let's go back to the counter class.
Let's go here
and create a new method called "Increment"
that we want to add 1 to the count.
Capitalize that.
"Count + 1".
Let's compile it, Cmd+S,
and you can see that the button is now on the left of the name of the method,
first because the system knows
that there is a test called "testincrement"
that is linked to this method.
We can just click on this one and now, it's green.
If you go to our CounterTest and press the button,
it is now running two tests and the two have passed,
so it is a good time to save our image once again.
Save.
The same way we defined increments,
we will go and define a test decrement.
It will run the same method.
"Decrement; Decrement", but now we want the count to be 0
because we subtract 1 twice.
If we compile, Cmd+S, the method appears
and if we run it, it should fail and it is failing
because it does not understand "decrement"
because we did not define it, so it is perfectly normal.
Let's just go back to the counter class and define "decrement"
so it should subtract 1 this time.
Once again,
it recognizes that there is a test associated to this method
with the button to the left.
We can just click on this one, it is green,
and now, if we click on the CounterTest,
it just runs all the tests from within it.
We can see 3 tests have been run, 3 passed.
It is a good time to save the image.
In this video, we learned how to create a class test
and how to define some methods.
Once again, test-driven development is encouraged from within Pharo
as it is really simple to write tests
and you will see in the next week how it is even easier to define methods
such as the methods decrement and increment that we just wrote.