Rory Becker - DevExpress CodeRush Blog

October 2016 - Posts

  • CodeRush for Roslyn – Writing Tests Fast

    So we’ve proved that CodeRush has the fastest .Net Test runner.

    Next up I’m going to show you that CodeRush is also the quickest way to write tests.

    Note: For the purposes of this post I’m going to assume that you’ve enabled Templates, and that you are using the Spacebar to trigger their expansion.
    If you have not enabled Templates, you can use the setup wizard to do so now.

    Writing Tests using CodeRush Templates

    So how simple could it possibly be to start writing a new test?

    How about t<space> ?

    That’s only 2 keys!

    Don’t believe me? Ok try this

    • Create a class library and add a reference to your favourite testing framework. I’m going to use NUnit which I’ve added via nuget.
    • Create a new class file and delete everything in it (Keep the namespace if you want.)
    • Choose an empty line and type the letter ‘t’ and then press the spacebar.

    You’ll be presented with a new test fixture with the skeleton of a new test.
    The name of the Test Fixture is already selected.

    • Change the name of the fixture to whatever you like.
    • Hit [enter] to confirm when you’re happy.

    Your selection moves to the name of the test.

    • Change that to whatever makes the most sense to you.
    • Hit [Enter] to confirm once again.

    Your caret is repositioned once again within the body of the test that you’ve just created.

    This is roughly what you can expect this experience to be like.

     CRR_Testing_TestWithFixture

    Effort

    If you count the characters I typed (t, [space], M, y, T, e, s, t, S, u, i, t, e, [Enter], M, y, F, i, r, s, t, T, e, s, t, [Enter]) You’ll come to 26 characters.

    However 22 of those were the names of my fixture and test, (which we’d have to type no matter what) so really we’re only talking about 4 characters. 2 to generate the frame of the test (and the fixture) and another 2 to confirm we’re happy with what we’ve typed and to move on to the next bit of defining our test.

    Whichever way you look at it…. That’s not a lot of effort.

    Complexity

    How easy or hard was that? Could you do it again? What do we have to remember?

    Well we have to remember that the test template is triggered with t and then [space] (or tab if you’ve got your preferences set that way).

    That’s literally it.

    Context

    Things get even simpler when you realise that you don’t always have to learn new template shortcuts for new purposes. CodeRush’s templates are context-sensitive. Which means that the same trigger can yield different expansions under different circumstances giving you even less to learn.

    For example, if you invoke the t<space> template from within a test fixture, CodeRush spots that you already have the fixture and only generates a new test.

    CRR_Testing_TestWithoutFixture

    Assertions

    Ok well that was simple. What else can we do?

    Well what do you need to do in a test when you get right down to it?

    • You setup a scenario.
    • You assert something about the scenario.

    Well your setup will be specific to your needs, but assertions are mostly the same.

    So does CodeRush have some tricks to help us assert a little quicker? …  Of course it does Smile

    So what sort of things do we typically assert?

    Well assertions (and therefore Tests) are all about checking something that should a certain way, and failing  (ie getting the developer’s attention) if that turns out not to be the case.

    We assert equality and inequality

    You Want To Template You Use Result
    Assert Equality ae<space>
    Assert Same as<space>
    Assert Not Same ans<space>

    …we assert that things are true and false

    You Want To Template You Use Result
    Assert True at<space>
    Assert False af<space>

    …and we compare with null

    You Want To Template You Use Result
    Assert Null an<space>
    Assert Not Null ann<space>

    As you can see CodeRush has you covered and always with a template that is intuitive and easy to remember.

    I’m sure you’re starting to see the benefits of CodeRush templates. I’ll leave you there for now, and let these templates soak in. Just know that no CodeRush templates are hard wired. They can all be altered or tweaked to your needs, and you can even create your own… We’ll explore how to do that in a future post.

    Get CodeRush for Roslyn Today

    If you’d like to try out CodeRush for Roslyn, a 30 day trial can be found here on the Visual Studio Gallery.

    CodeRush for Roslyn can be yours for as little as $49

LIVE CHAT

Chat is one of the many ways you can contact members of the DevExpress Team.
We are available Monday-Friday between 7:30am and 4:30pm Pacific Time.

If you need additional product information, write to us at info@devexpress.com or call us at +1 (818) 844-3383

FOLLOW US

DevExpress engineers feature-complete Presentation Controls, IDE Productivity Tools, Business Application Frameworks, and Reporting Systems for Visual Studio, along with high-performance HTML JS Mobile Frameworks for developers targeting iOS, Android and Windows Phone. Whether using WPF, ASP.NET, WinForms, HTML5 or Windows 10, DevExpress tools help you build and deliver your best in the shortest time possible.

Copyright © 1998-2017 Developer Express Inc.
All trademarks or registered trademarks are property of their respective owners