This tutorial looks at some unit test tips that are sure to make you more productive. See how to clear the test explorer of stale tests. Learn how to configure Test Explore to run the test after every build. Discover how to run a subset of tests by using the search and filter features in Test Explorer.
- View Offline
- [Voiceover] In this video, I'll look at some Unit Test tips to make me more productive in Visual Studio. The first tip, is to enable the Auto Test feature. This runs the Unit Test after every successful build. This way the tests are automatic, at least on your computer, and you can quickly catch and test errors after you make code changes. To enable the feature, go to the Test menu. Choose Test Settings, and then click on this first menu item. This is a Toggle menu. When you click once, it enables the feature. The next time you click on it, it disables it. It's currently disabled.
Look at this icon here. Now, click on the item. See, now there's a yellow border around that icon, that means it's enabled. Before I show you the feature, let's go head and run my Unit Test. Now you notice when you run your Unit Test, you get this moving bar at the top of your window, and then you get the results, so keep that in mind. Also at the bottom of the window, there's an up Output window. One Output window for build, and there's another Output window for tests. So, I can see the results of my tests here. I'll clear this list.
Then I'll go over to my code, make a change, save my file, and I'll recompile my application. Notice that I'm getting the moving bar here, at the top of the Test Explorer. That means I've built. It's now running my tests, and again if I verify by going and looking in tests, you see that it ran those tests. What do you think will happen now, if I don't make any code changes, and I do a build? There's no moving bar, if I go look in my test window, there's nothing there.
If I look in the Build Output, you see that my project is up-to-date. There were no code changes, so there's no need to run the Unit Test, because nothing has changed. Tip number two, is to work with the way that you group your items. Now, I've already showed how to work with Classes. This is grouping by class names. This is grouping by Project. And I've also seen how to group by Traits. These are things like Test category, or the Owner Property. There's two that we haven't looked at yet.
One of them is Duration. This splits my code into different groups: fast, slow, medium. You see I've got medium code, ran in more than 100 milliseconds. Fast is ran in less than 100 milliseconds. And two of my tests didn't run at all. Before I run the next test I'll go down here, and uncomment this line. This will cause a failing test. Compile my project, which will run my Unit Tests. Now, I'll show you the last grouping, Group by Outcome.
This shows me, my failed tests in one section, my skipped tests in another, and all my past tests down here at the bottom. Grouping my items is nice, but I still see all the tests. So if I have 5000 tests, I'll still see them in the window. So there's a way to filter down your list. It's not obvious where it is. For one, you can do a search. So I can come up here and look for Connect. And that shows me just the two past tests that had the word connect in the test. So, now I can run just these tests.
And less obvious, is if you go over to this drop down here. There's a lot of other filters. So I can filter by Class, by Trait. Let's say, I want to filter by this test category Network. So what I do is I go up to drop down and choose Trait. So it says, "Connect Trait." Get rid of the connect part, and then it's after the colon and inside the double quotes.
I put in the word that I am looking for: Network. I can also search by Outcome, Failed. Now, I'm doing a double search, and since there's no outcome that failed there's also a Trait equal Network. Let me remove this trait here. And then you'll see it brings it in, and it shows me my Failed test. I'll turn off the filter.
We're back to looking at all my tests. Run all those. And then I'll talk about the last thing, which is how to clear out the tests. So I'm looking at my tests, and I can see I've got eight tests here. There are times when you want to clear this entire list. You don't want to see anything anymore. The way you do that, is you go to the Build menu, and you choose Clean Solution. These are a few of the tips that make me a happier Unit Test Developer in Visual Studio.
- Examining types of frameworks
- Choosing a naming convention
- Creating unit tests
- Running unit tests with Visual Studio
- Modifying and correcting code
- Handling exceptions
- Installing and using nUnit
- Viewing test results with CodeLens