microsoft / UnitTestBoilerplateGenerator

An extension for Visual Studio that generates a unit test boilerplate from a given class, setting up mocks for all dependencies. Supports NUnit, Visual Studio Test, Moq and SimpleStubs.
MIT License
158 stars 51 forks source link

Edit Template for Test method #45

Closed DMEvans closed 5 years ago

DMEvans commented 5 years ago

Installed product versions

Description

It would be great to have the option to pre-format the test method template the Options menu the same as we currently can with the class declaration.

Expected behavior

Menu > Tools > Options.... Select "Unit Test Boilerplate Generator" Have Option for "Test Method Contents" This will provide a similar set of options to the "Test File Contents" options to allow the test method template to be edited

RandomEngy commented 5 years ago

What would you want to change about the test method contents? There's currently a lot of code that's run to build it and templatizing certain parts like the argument variable creation or the invocation line might be difficult.

KerbalSpike commented 5 years ago

Well for me the biggest complaint about the test method contents are the lines like

int a = TODO;
int b = TODO;

because they do not compile right away.

So when I have created a new boilerplate test class and wrote the first proper test, then I can't run the test right away because the class doesn't compile until I fix (or comment out) all the other test method stubs.

KUTlime commented 5 years ago

I strongly agree with @DMEvans.

I big fan of TDD development and I've tried many different approaches how to organize UTs over years.

I settle up with following UT organization:

namespace SomeClassNameSpace.UnitTests
{
  public class SomeClassTests
  {
    [TestClass]
    public class SomeMethodInSomeClassTests
    {
           [TestMethod]
           public class SomeTest
       {
            // AAA goes here...
       }

       [TestMethod]
           public class SomeOtherTestForSameMethod
       {
            // AAA goes here...
       }
    }

    [TestClass]
    public class SomeOtherMethodInSomeClassTests
    {
           [TestMethod]
           public class SomeTest
       {
            // AAA goes here...
       }

       [TestMethod]
           public class SomeOtherTestForSameMethod
       {
            // AAA goes here...
       }
    }
  }
}

BTW: This system is not mine, I adopted based on this article.

I couldn't do it with this extension. No possibility to customize the test generation within the methods part of the process. I had to create my own fork.

IMHO: This system should be used as default or optional setup at least. I can provide my fork for this purpose.

RandomEngy commented 5 years ago

@KUTlime I've started adding the "test method" template, which would allow full customization. If you could share your fork it might be handy in implementation.

KUTlime commented 5 years ago

@RandomEngy David, sorry, I am on holiday for 8 weeks without my PC. I won't be able to share my fork but what I did what basically editing the testmethod generation and added couple of lines to customize it according my needs.

RandomEngy commented 5 years ago

@KUTlime Have fun on holiday. It sounds like the fork you did wouldn't be terribly useful to me anyway as I'm planning to templatize the whole thing.

KUTlime commented 5 years ago

@RandomEngy You are absolutely right. :-) I've just added couple lines to code and that made the trick. Your approach is more robust. I can help you but I won't be available to code anything until September. Let me know if I can help you somehow in September.

MikeKry commented 5 years ago

@RandomEngy

Hello, will there be also possibility to change codestyle for private fields etc?

RandomEngy commented 5 years ago

@MikeKry What do you mean by "private properties"? You mean the private fields for the mock objects? You can already do that on the Options page.

RandomEngy commented 5 years ago

Just released v2.0, where you can customize the test method contents with a template. It also will generate default values for the method parameters, to allow you to build and run the tests right away.

RandomEngy commented 5 years ago

Also give the extension a review if you like it. :)