So here we are in Visual Studio 2022, and I will now create a new project and you should do that too,
if you are following along with that course.
And I really think that you should do that.
So don't only sit there and watch the video.
Please go ahead and open up Visual Studio and create a project and follow along in a more practical
way.
Our goal at tutorial SEO is to provide you the very best learning experience, and that for sure, also
needs some actions from your side.
So you will need to get active too.
Now let's create a new project and we are using a testing framework which is called X Unit.
So simply search for X unit and create an X unit test project.
And that one got originally also created from the author of N unit.
You might know that testing framework or maybe you already heard the term.
However, X unit also runs on Dotnet core.
You can see that here on Dotnet core Windows Linux and Mac OS.
So simply go ahead, select that one, click on next and let's call it calculator now hit next and select
dot net six and hit create.
Let's write the sum function which we have just discovered and defined in one of the last videos.
So simply write down here and sum.
So we create a method and we got int left and integer for right side.
Now let's simply return left right I taking place here.
So in fact we simply created a method of return type integer which takes two parameters.
The first one is called left, the second one is called right.
And in the end we just sum them up right and return the actual result of it.
So right now we have simply created a C sharp default method.
Now take a look at the C sharp script that we have here.
We have a class called Unit Test one, and here we have an attribute which is called fact.
So if we now want to go ahead and automatically test our new sum method here, the only thing that we
have to do to make that really quick is to create an if statement.
Let's say if sum of two and two is on equals to four, then we want to go ahead and throw in new exception
system exception.
That's fine in case we haven't included the namespace, but I want to do that because I want to keep
the code as short as possible.
So let's go using system.
So this one is no longer used.
Okay, so here we have a test method which is indicated by the fact attribute.
We will get to that just in a second.
And inside here we call our sum method and we check if the result two and two is not four, but it should
be.
And in every other case.
So anything is wrong with our method, we will throw an exception because the only thing that this method
here has to do is to sum two numbers and two and two should be four.
And if it's not four, the algorithm inside here would be wrong.
Right now it's just a very simple algorithm, right?
It's just addition of two numbers.
However, we also need to make sure that our testing condition is right.
So some two and three right.
That will be a wrong testing condition because our defined statement is wrong at all.
So always make sure that your testing condition matches the actual result that you are expecting.
Let's save it like this.
Okay.
Now I want to show you how you can run an actual test, because right now you might think, okay, I
can test it when I hit the green play button or start button at the top side of the screen.
But that's wrong.
You can start the test and you can run the code test by right clicking on the method and simply hit
run tests.
Now let's do that.
A new test explorer window will appear and you can see that the tests are now processing.
Let's give it just a second to get everything done.
And you can see the result.
Is that our calculator?
Let's open it up.
Unit test class test one.
This is exactly the name of the test method here.
Right?
The naming.
We will change that just in a second.
But that's the method that got tested and it actually succeeded.
So this was a successful test.
You can see that here.
One passed.
So if we do this like some two and three, right?
So this would be wrong.
Let's simply save it and rerun the test.
Here you can see run all tests in view and simply run.
Let's let me hit run again.
You can see it failed because, well, we said that it failed.
Two and three is not four.
So we throw an exception.
And throwing an exception from a testing method is a failure.
So let's simply correct that again.
Two and two.
Let's set it again.
So this is how you can create a test method and how you can actually start it.
And as we have seen, we have two outcomes or two possible outcomes which is actually passed or failed.
Congratulations.
You just have created your very first test method.
Now let's get rid of this bad naming convention here.
Let's refactor our project in a very professional way.
How you would do it as a professional software engineer.