4

In a typical Java IDE (say Intellij), working Test Driven Development (TDD) style mean a very efficient workflow and virtually no modifications or alternations are needed after you finish the code through the test.

Java projects, just as C# solutions, have various styles of separating production code from test code, but usually there are two directory structures in parallel

  • src
  • test

or the tests are separated by namespace in the src tree. And then endless variations on that theme.

Using something like Intellij I can create my "test class/method", start coding, generate objects on the fly, as I go, and they generally end up in the correct place, that is, where the production code is supposed to be. When I'm done, I'm done done. Tools and IDE's in the Java world support this 110%.

Now the experience Visual Studio (or actually the R# experience). Currently my (simplified) workflow for doing TDD in C# is something like this (using R#):

  1. Create a test class in a test project
  2. Create test method
  3. Start coding.. generate classes, interfaces, etc as I go. I generate the code in the same file as the test-class as per R# default. ...
  4. When I'm done I go to each class/interface/whatever and move the files into separate files matching their name (ctrl+enter action).
  5. Go to solution explorer and drag-drop the classes from the test-project over to the production project.

I want to get rid of everything 4 & 5. And I would also like my classes to generate directly in the production project in step 3.

I've searched high and low for an extension to Visual Studio that can help me with this.

Question 1: How can I get more productive with TDD the way I do it today?

Question 2: Should I adopt another TDD workflow/structure that would ultimately provide me with a better TDD experience in Visual Studio? (this is an alternative question)

Remain unanswered But please take the time to vote for this feature in Resharper https://youtrack.jetbrains.com/issue/RSRP-424370

gurun
  • 134
  • 11
  • Downvoter please explain? This is a good question, does anyone know how to accomplish this with the [Generate Method Stub](http://msdn.microsoft.com/en-us/library/tcz8b6zc(v=vs.90).aspx) - ie direct where (solution\project\class) the stubs should created? To me this feature seems like a great idea for the guys over at JetBrains. – Jeremy Thompson Sep 16 '14 at 22:12
  • Start with creating separate files, then use Window -> Split. – mxmissile Sep 16 '14 at 22:15
  • @mxmissile please elaborate how using split window will avoid step 4 & 5. I am interested! – Jeremy Thompson Sep 16 '14 at 22:19
  • 1
    I don't understand the reason for doing 3 that way. Why are you not creating production code where it will ultimately go? – Brian Rasmussen Sep 16 '14 at 22:27
  • @BrianRasmussen I'm not sure how to address that question. Maybe I'm extreme about TDD, but I actually expect that when I write the code, it doesn't exist and I expect the IDE to just just "do what I tell it to do". I mean, the editor can figure everything out itself, so why would I have to do it. Call me lazy, but I believe it is one of the keys to being very pragmatic about coding. – gurun Sep 16 '14 at 22:41
  • @BrianRasmussen How about this explanation: When I do TDD I don't want my brain to be limited by fear of waste. So if I write code that would ultimately require me to create a new class by hand in the production project I know that this will change my behaviour to be less "creative" about what I TDD code. – gurun Sep 16 '14 at 22:44
  • ReSharper doesn't offer anything automatic for this. The workflow you describe is the norm. Out of curiosity, though. How does IntelliJ know when a class you create is a test class or a production class? How does it handle a helper class in a test project? – citizenmatt Sep 17 '14 at 09:42
  • @JeremyThompson You start with #4, then #5. Files/classes are created where they should be. – mxmissile Sep 17 '14 at 13:32
  • @citizenmatt Intellij assumes that your "intention" is to create a production class/interface/etc, however when you press and choose to create a new class in the intention part of the menu, it will instantly popup a small dialog asking you for 1) wanted namespace (pre-filled) and location (prefilled with the prouduction root). Doing the same in visual studio with Resharper give you no opotions other than create class or nested class. They are generated in place. – gurun Sep 17 '14 at 15:01
  • @gurun that is a shame. I share your frustration with this problem. I usually move the class from the test project to the production project as soon as it's created rather than after creating the members, but that doesn't really solve the underlying problem. – phoog Sep 17 '14 at 22:42

1 Answers1

2

You can remove steps 4 & 5 with Visual Studio using Generate Method Stub feature, the Resharper bit distracted me.

Create two projects:

Solution
| Project1
_ Quiz.cs
| UnitTestProject
_ UnitTestClassQuiz.cs


1. Create an empty class called Quiz in Project1.
2. Create a unit test in UnitTestClassQuiz.cs , eg:
using Project1;
private class UnitTestClassQuiz
{
[TestMethod]
public void ShouldReturnDivisibles()
{
  //Arrange
  Quiz q = new Quiz();

  //Act
  var actual = q.ReturnDivisibles(60) 

At this point VS will have a squiggle red line under the 'ReturnDivisibles' function because its undeclared! Hover the mouse over this undeclared 'ReturnDivisibles' function and you will see a little blue underline/underscore, click on it and it will give you a context menu with the option to "Generate Method Stub".

When you generate the method stub a function will be created in the Quiz class in Project1, eg:

public object ReturnDivisibles(int p)
{
  throw new NotImplementedException();
}
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • Sorry Jeremy, but I don't want to create the class manually like you did in the example. Resharper already works as you describe. Getting rid of 4 & 5 ultimately requires that this is automatically created in the right place in step 3 (which it is not). – gurun Sep 18 '14 at 14:27
  • *I dont want to create the class manually* - I think you might have to compromise this (step 3). Without manually creating the class to be tested in the desired project how would Visual Studio, Eclipse, ReSharper, etc know where to generate the method stubs? – Jeremy Thompson Sep 21 '14 at 02:32
  • I compromise with this everyday right now. In IntelliJ, it pops a small dialog asking where, but with defaults set to production and with proper namespace so its basically a matter of pressing enter. It should work exactly as the visual studio extension for creating tests, but reverese. That extension defaults to assumptions by convention. – gurun Sep 21 '14 at 12:43
  • Look into VS Extensions, shouldn't be too hard to whip this up :) – Jeremy Thompson Sep 22 '14 at 01:13
  • Please vote for this issue over at atlassian - https://youtrack.jetbrains.com/issue/RSRP-424370 – gurun Feb 17 '15 at 12:19