views:

368

answers:

8

I am looking for a tool that can take a unit test, like

IPerson p = new Person();
p.Name = "Sklivvz";
Assert.AreEqual("Sklivvz", p.Name);

and generate, automatically, the corresponding stub class and interface

interface IPerson         // inferred from IPerson p = new Person();
{
    string Name 
    { 
        get;              // inferred from Assert.AreEqual("Sklivvz", p.Name);
        set;              // inferred from p.Name = "Sklivvz";
    }
}

class Person: IPerson     // inferred from IPerson p = new Person();
{
    private string name;  // inferred from p.Name = "Sklivvz";

    public string Name    // inferred from p.Name = "Sklivvz";
    {
        get
        {
            return name;  // inferred from Assert.AreEqual("Sklivvz", p.Name);
        }
        set
        {
            name = value; // inferred from p.Name = "Sklivvz";
        }
    }

    public Person()       // inferred from IPerson p = new Person();
    {
    }
}

I know ReSharper and Visual Studio do some of these, but I need a complete tool -- command line or whatnot -- that automatically infers what needs to be done. If there is no such tool, how would you write it (e.g. extending ReSharper, from scratch, using which libraries)?

A: 

I find that whenever I need a code generation tool like this, I am probably writing code that could be made a little bit more generic so I only need to write it once. In your example, those getters and setters don't seem to be adding any value to the code - in fact, it is really just asserting that the getter/setter mechanism in C# works.

I would refrain from writing (or even using) such a tool before understanding what the motivations for writing these kinds of tests are.

BTW, you might want to have a look at NBehave?

Carlos Villela
+1  A: 

If you plan to write your own implementation I would definately suggest that you take a look at the NVelocity (C#) or Velocity (Java) template engines.

I have used these in a code generator before and have found that they make the job a whole lot easier.

FryHard
A: 

I use Rhino Mocks for this, when I just need a simple stub.

http://www.ayende.com/wiki/Rhino+Mocks+-+Stubs.ashx

Hibri
This is how I do it, says so much code and time!
Chris Canal
It doesn't generate files... I want the stubs to be created so I can then modify them manually.
Sklivvz
A: 

Visual Studio ships with some features that can be helpful for you here:

Generate Method Stub. When you write a call to a method that doesn't exist, you'll get a little smart tag on the method name, which you can use to generate a method stub based on the parameters you're passing.

If you're a keyboard person (I am), then right after typing the close parenthesis, you can do:

  • Ctrl-. (to open the smart tag)
  • ENTER (to generate the stub)
  • F12 (go to definition, to take you to the new method)

The smart tag only appears if the IDE thinks there isn't a method that matches. If you want to generate when the smart tag isn't up, you can go to Edit->Intellisense->Generate Method Stub.

Snippets. Small code templates that makes it easy to generate bits of common code. Some are simple (try "if[TAB][TAB]"). Some are complex ('switch' will generate cases for an enum). You can also write your own. For your case, try "class" and "prop".

See also "How to change “Generate Method Stub” to throw NotImplementedException in VS?" for information snippets in the context of GMS.

autoprops. Remember that properties can be much simpler:

public string Name { get; set; }

create class. In Solution Explorer, RClick on the project name or a subfolder, select Add->Class. Type the name of your new class. Hit ENTER. You'll get a class declaration in the right namespace, etc.

Implement interface. When you want a class to implement an interface, write the interface name part, activate the smart tag, and select either option to generate stubs for the interface members.

These aren't quite the 100% automated solution you're looking for, but I think it's a good mitigation.

Jay Bazuzi
Thanks, I already use those tools, but what I'm interested in is way more sophisticated, for example it should infer from the assignment and the assert that the property is read write
Sklivvz
A: 

I like CodeRush from DevExpress. They have a huge customizable templating engine. And the best for me their is no Dialog boxes. They also have functionality to create methods and interfaces and classes from interface that does not exist.

adriaanp
+2  A: 

Its amazing how no one really gave anything towards what you were asking.

I dont know the answer, but I will give my thoughts on it.

If I were to attempt to write something like this myself I would probably see about a resharper plugin. The reason I say that is because as you stated, resharper can do it, but in individual steps. So I would write something that went line by line and applied the appropriate resharper creation methods chained together.

Now by no means do I even know how to do this, as I have never built anything for resharper, but that is what I would try to do. It makes logical sense that it could be done.

And if you do write up some code, PLEASE post it, as I could find that usefull as well, being able to generate the entire skeleton in one step. Very useful.

mattlant
+1  A: 

It's doable - at least in theory. What I would do is use something like csparser to parse the unit test (you cannot compile it, unfortunately) and then take it from there. The only problem I can see is that what you are doing is wrong in terms of methodology - it makes more sense to generate unit tests from entity classes (indeed, Visual Studio does precisely this) than doing it the other way around.

Dmitri Nesteruk
+1  A: 

Try looking at the Pex , A microsoft project on unit testing , which is still under research

research.microsoft.com/en-us/projects/Pex/

vijaysylvester