DEV Community

Paweł Ruciński
Paweł Ruciński

Posted on

Accelerate your test creation

I want to code efficiently. I am always trying to use TDD approach. For some time I have been wondering, what takes up most of my time during development process. I want to improve my daily routine, to care more about coding itself and less about configuring solutions, projects, classes etc.

Introduction

I am aware of power of scaffolding, but really, how often do you launch brand new solution? When creating, lets say web solution, you have to decide if it should be only an API or/and a front-end solution, right?
Creating new MVC project
After some time, there is a nice setup solution. With created HomeController, related Views etc. You have something to work with instead of writing everything from a scratch.
Created solution

But it happens once for a solution lifetime. From the other hand, how often does it occur that you need to write new lib? If you are practicing TDD, there is also an associated test project.

So I was wondering, is there any way to take this functionality and apply it on creation of test project? Because each time I create a class library, I also create a test project. I am using XUnit on my daily basis, so I focused on using it in my little invention.

First attempt

I did some research to know if there is any option. Scaffolding is a well known functionality, introduced in Visual Studio 2013. It is not so difficult to create custom scaffolded items. There is a way to extract it from existing projects, files, code. Following this way, I created my first templates for Visual Studio. I took vs15 and vs17. You can find my work here. It is nothing advanced, but it fulfills my basic requirements. Trying to be professional, I have made an icon and an installer for this. It creates a project with XUnit nuget preinstalled or a new class with imposed test method layout.
Scaffolding for XUnit

It results in a test layout like this below:

using Xunit;

namespace Xunit_Test_Project1
{
    public class Tests
    {
        [Fact]
        public void Method_When_Should()
        {
            // arrange

            // act

            // assert

        }
    }
}
Enter fullscreen mode Exit fullscreen mode

I also created a code snippet for creating test methods - works same as above after typing testmethod. There is also a possibility to do that with R# mechanisms.

Feedback

Next morning, full of the joys of spring, I went to the office and wanted to share my little side project with co-workers. First person I had shared the news with, brought me down to earth. There is already created solution, which provides everything what I need.

My colleague showed me NUnit.TestGenerator. Did you know that? He was working with in in his previous company. After playing with it for some time, I agreed that it was very useful and this was what I really needed. My nature didn't give up and was trying to pick holes in this Nunit.TestGenerator. Why? Because it is using NUnit instead of my favorite XUnit. So I started to dig for any equivalent using Xunit.

Test generator for xunit

Unfortunately, none of these is available on Extension and Updates context menu.

Second attempt

Bearing this in mind for a few days I have been searching for similar solution. I found XUnit.TestGenerator online, author - YowkoTsai. It is not available through VS, but you can download an installer.

It works like this - after right-click a method name, shows up a menu, where is a section for test purpose. Without this add-on, there are only Run tests and Debug tests options. When you install it, you can also find Create unit tests options.
Create tests
It opens a new dialog window, where you can configure your whole new project and/or classes by clicking on it.
Create test context

Do you think that the journey already ends? Noooooo.

I found it also on github. Forked it. I didn't touch it during first week, but after that, I sat down and tried to enforce a few of my rules for testing.

  • Test project naming convention: .Test and .IntegrationTests
  • Test method naming convention: Method_When_Should
  • Test method layout (as above)
  • XUnit of course!

I started with the easiest one, test layout. This was a simple change on one field. Build, install and it works. At least something I was able to customize to. I realized that other points were not possible, because of the lack of the documentation, because only a few repository exists on github, etc. Even if I wanted to do that in a proper way, it would take so much time with testing, building and so on. Now I am wondering if Microsoft will eventually give us an opportunity to fully customize tools like this. After all I end up with something like this (slow loading):
gif

Summary

I will use my custom test generator. Also, I want to try to convince my co-workers to use it as well. I like to use shared standards in daily work. Is it useful? Will I benefit from it?

I hope that time will show.


What are your feelings about this scaffolding in general? Do you have favorite tools for creating tests? Can you help me to push further this customization?

Please, share with me your experience.

Top comments (4)

Collapse
 
andreujuanc profile image
Juan C. Andreu

You should really add the ability to add all the tests needed in a whole solution. yes clicking one by one is nice, but it'd be nice to use something like that when you pick up someone else's project and you suddenly find zero unit tests. So instead of clicking methods one by one, you can just show a list of what's needed and proceed.

Also it'd be nice for cases when you don't do TDD but want to create tests afterwards.

BTW, integration tests templates are very much needed in VS.

Collapse
 
meanin profile image
Paweł Ruciński • Edited

ability to add all the tests - Totally agree with that. Unfortunately, as far as I know there is no tool for that. I will consider creating one in the future.

Thanks for comment!

Collapse
 
andreujuanc profile image
Juan C. Andreu

Please ping me when you have a beta version for that, i might give it a try!

Collapse
 
j_sakamoto profile image
jsakamoto

The test generator is great work I think.

I also had been writing a code snippets pack, a project template, and an item template for xUnit.net like this: Visual Studio Marketplace - xUnit Code Snippets