Resharper for Visual Studio: Build Amazing Automation Testing Code

Something amazing happened to me recently! I am always learning, growing and exploring other resources. And recently I stumbled upon an amazing Visual Studio productivity tool called ReSharper. According to JetBrains, the creators of ReSharper, "ReSharper makes Microsoft Visual Studio a much better IDE. .NET developers worldwide wonder how they've ever lived without ReSharper's code inspections, refactorings and navigation."

From my first usage, I can claim that this assertion is completely correct. Within a few minutes of installation I had ReSharper improving my code in countless ways. It was simply phenomenal!

By the way, I am not an affiliate of any sort for this company. I just freaking love the tool and wanted to share the awesomeness with my fellow Software Testers.

What is ReSharper?

ReSharper is a tool that can be plugged into Visual Studio to help improve its IDE. There is a ton of benefits that can come from ReSharper that we will discuss later.

This is the site with their download link. You get a Free 30 day trial.

Why Should Automation Engineers Use ReSharper?

As I stated before, I personally love this tool. It simply makes your code much better without having to spend hours refactoring. It helps to analyze your code during design time and helps you to fix tons of different errors and warnings. ReSharper removes redundant code, unused variables and unused references. ReSharper helps to keep you following the Microsoft standards of using C#. ReSharper makes your code easier to understand and maintain. It even helps to improve your logic so that it is easier to follow in the future.

After about 1 hr of refactoring, the code in my automation testing framework was drastically different! Only in good ways. A bunch of explicit casts were replaced to implicit casts with the click of a few buttons. Several complicated nested loops and statements were replaced with much more efficient logic. So many old references were deleted from my solution to make for a much lighter piece of architecture. The coolest part is that I am actually learning from this tool. Some C# Coding Standards and different ways to refactor logic are some of the most drastic lessons that I learned.

Basically, it's saving my employer time and money while teaching me some cool information. Now my automation testing framework code is as sweet as Organic Raw Honey! Let's really dig into some of the awesome features.

 

Best ReSharper Features

Keep in mind that I just started utilizing this tool. Therefore, there may be tons of other amazing features that I am not aware of. However, since the beginning, these are the ones that blew the pants off of me.

ReSharper has continuous code quality analysis for a bunch of different languages. I'm currently using C# so that's the one I cared about.

  • It has over 1700 different code inspections. It alerts you in the sidebar of the different errors at design time.
  • Besides all of the typical errors and warnings that Visual Studio provides, ReSharper also assists you by giving you warnings about redundant casts, incorrectly formatted strings, never used variables and code that cannot be reached. This feature is so awesome because it helps you to clean up your code. It has helped me to cut down my code by 100s of lines by removing unused variables. Also, if you have conditions that will never be met, ReSharper will let you know so that you fix your logic.
  • ReSharper can also give you Hints that can help you to improve your code. 
    Hints
    Shares
    • Facebook
    • Twitter
    • Google+
    • Buffer
    • Gmail
    • Delicious
    • reddit
    • StumbleUpon
    • Tumblr

 

Get This Code

Let's dive deep into some real world examples with code so that you all can get a full understanding with some much required practice. Have you ever faced a situation where you needed to update the signature of the method and that method has 20 references? I sure have and it's a pain in the gluteus maximus. ReSharper makes this process so easy. Take a look at this example:

  1. Here I have a very simple Person class that takes 1 argument for person's name.
    1-automation testing-Person class
    Shares
    • Facebook
    • Twitter
    • Google+
    • Buffer
    • Gmail
    • Delicious
    • reddit
    • StumbleUpon
    • Tumblr
  2. I also created a console application that creates 2 instances of this class. The calls simply pass in some names for the Person class.
    2-automation testing - Calling the Person class
    Shares
    • Facebook
    • Twitter
    • Google+
    • Buffer
    • Gmail
    • Delicious
    • reddit
    • StumbleUpon
    • Tumblr
  3. Now, the very common scenario! What if I wanted to update my Person class to take in more than 1 parameter? Well, I can obviously override the method to create another one with more parameters. But what if that's not what I wanted or needed for some other reasons? So, in this case, I am going to add an "age" and a "sex" to the class constructor.
  4. Notice how after compiling, I have 2 new errors. Makes sense right? Since I have 2 calls to this constructor and I just changed the method signature, each of those calls is going to complain. If I had 50 calls to this constructor, I would have 50 errors!
    3-automation testing- 2 new params added to Person
    Shares
    • Facebook
    • Twitter
    • Google+
    • Buffer
    • Gmail
    • Delicious
    • reddit
    • StumbleUpon
    • Tumblr
  5. Without ReSharper, you would have to navigate to every single error and update the calls to have 2 new strings. What a pain in my muscular butt! However, with ReSharper, it's easy.
    1. First, update your method to have 2 new arguments. Without building the code, you will notice a dashed highlight around the new method signature. Use "Alt + Enter" to open the ReSharper help menu.
      4-1-automated testing- initial highlight upon change
      Shares
      • Facebook
      • Twitter
      • Google+
      • Buffer
      • Gmail
      • Delicious
      • reddit
      • StumbleUpon
      • Tumblr
    2. You should see an option from the refactoring menu to "Apply change signature refactoring...". Press enter to open that menu.
      4-2-automated testing-opening the refactoring menu
      Shares
      • Facebook
      • Twitter
      • Google+
      • Buffer
      • Gmail
      • Delicious
      • reddit
      • StumbleUpon
      • Tumblr
    3. A "Change Signature" pop up wizard will open to guide you through the refactoring. Press "Next".
      4-3-automated testing-refactoring pop up wizard
      Shares
      • Facebook
      • Twitter
      • Google+
      • Buffer
      • Gmail
      • Delicious
      • reddit
      • StumbleUpon
      • Tumblr
    4. Now, a magical thing will happen where the wizard will guide you through every parameter to help you refactor your code. After you are finished, ReSharper will assign values to your constructor calls based on the options that you selected.
      4-4-automated testing-default age value setting
      Shares
      • Facebook
      • Twitter
      • Google+
      • Buffer
      • Gmail
      • Delicious
      • reddit
      • StumbleUpon
      • Tumblr
    5. There are multiple options for each parameter. They are basically self explanatory. However, for "age", we will use a specific value of 25.
      4-5-automated testing-set the age to a certain value
      Shares
      • Facebook
      • Twitter
      • Google+
      • Buffer
      • Gmail
      • Delicious
      • reddit
      • StumbleUpon
      • Tumblr
    6. Click "Next" after your settings look like the image above.
    7. For "sex", let's leave it as non-compilable so that we can resolve the issue ourselves. Click "Next".
      4-7-automated testing-select for sex to be left as non compiled
      Shares
      • Facebook
      • Twitter
      • Google+
      • Buffer
      • Gmail
      • Delicious
      • reddit
      • StumbleUpon
      • Tumblr
    8. Now build your code. In Visual Studio 2013, I can just press "F6" to build. In earlier version, this shortcut is different. Also, you can build the code by going to the "Build" tab and then clicking "Build Solution".
      4-8-automated testing-how to build the code
      Shares
      • Facebook
      • Twitter
      • Google+
      • Buffer
      • Gmail
      • Delicious
      • reddit
      • StumbleUpon
      • Tumblr
    9. As expected, we will have 2 errors. 1 for each of the "sex" parameters that have no value. We can go to each error by double clicking on the error in the "Error List" pane of VS.
      4-9-automated testing-post build errors
      Shares
      • Facebook
      • Twitter
      • Google+
      • Buffer
      • Gmail
      • Delicious
      • reddit
      • StumbleUpon
      • Tumblr

    10. Look at what ReSharper did. That genius added "TODO" statements in place of the argument values so that you don't forget to replace the "TODOs" with actual values!
      4-10-automated testing-resharper added todo statements
      Shares
      • Facebook
      • Twitter
      • Google+
      • Buffer
      • Gmail
      • Delicious
      • reddit
      • StumbleUpon
      • Tumblr
    11. Update the code to look like the image below. Also, build it after and notice how all of the compiler errors are gone.
      4-11-automated testing-fixed code with no errors
      Shares
      • Facebook
      • Twitter
      • Google+
      • Buffer
      • Gmail
      • Delicious
      • reddit
      • StumbleUpon
      • Tumblr
    12. Congrats! ReSharper has just successfully refactored your method calls without you having to go through every error and updating it yourself. Use it in the future and you will save yourself tons of time.

Another Code Refactoring Example

So, continuing with this code base, we can further clean up.

  1. Notice all of the unused directives that are highlighted in gray. ReSharper is telling us that they can be removed.
    5-1-automated testing-unused directives
    Shares
    • Facebook
    • Twitter
    • Google+
    • Buffer
    • Gmail
    • Delicious
    • reddit
    • StumbleUpon
    • Tumblr
  2. When you are ready to refactor and you want to go through each Warning 1-by-1, click on the Warning icon on the right side of your VS, in the upper right corner.
    5-2-automated testing-warning icon
    Shares
    • Facebook
    • Twitter
    • Google+
    • Buffer
    • Gmail
    • Delicious
    • reddit
    • StumbleUpon
    • Tumblr
  3. It will take you to the first one and place a lighbulb next to the line of code. Also, ReSharper will place your cursor on that line as well.
    5-3-automated testing-light bulb
    Shares
    • Facebook
    • Twitter
    • Google+
    • Buffer
    • Gmail
    • Delicious
    • reddit
    • StumbleUpon
    • Tumblr
  4. Just like before, press "Alt + Enter" to open the ReSharper menu and select from the options.
  5. Select "Remove unused directives in file...". This step will clean up all of the using statements from your code that are not being used. Your code will now look so sleek and clean.
    5-5-automated testing-code without directives
    Shares
    • Facebook
    • Twitter
    • Google+
    • Buffer
    • Gmail
    • Delicious
    • reddit
    • StumbleUpon
    • Tumblr
  6. By the way, ReSharper will also do the same thing with References that are not being used. You can remove them at the file level, project level or solution level with just a few clicks. This will make your code lighter and faster.

 

Code Refactoring - Sample 3

We can make 1 more awesome fix to the code using ReSharper's big brain. ReSharper lets you know if there are variables that are never used.

This really saved my life recently. I was creating an automated test in Selenium WebDriver and copied over an assertion message. Because the message contained a lot of information, I stored it in a variable. I changed the variable name but never updated the variable that was being used in the assertion clause. As a result, I was checking 1 condition, but my assertion message was for a completely different check. I don't know if I would have ever found this problem.

But ReSharper came to my rescue. It told me that I had a variable that I was never using. After a further look, I realized the problem and fixed it with a huge sigh of relief! The message looks like the image below. When you see these warnings, you can decide what to do with the variables. You can use them or delete them using ReSharper's IDE. This helps you to clean up your code or find silly mistakes like the one that I made.

 

Wrapping Up!

I hope that I have been successful at conveying to you the awesomeness of ReSharper? I even showed it to 1 of our developers. This guy is really freaking smart, but also really freaking angry. He was actually impressed with how ReSharper helped us to refactor. Now that is a fact that has more weight than 10,000 words on my real examples of ReSharper features! So give it a try and let me know what you think. If you completely disagree with me, let me know why. If you love some other features, please share so that I can learn as well. The only drawback that I saw so far was that ReSharper makes my IDE slower. But, it's worth that small performance degradation.

Talk to you all later. Peace, love and automation!

Get This Code

Author: Nikolay Advolodkin, Automation Engineer with a passion for Automated Software Testing.

Nikolay Advolodkin

Test Automation Engineer at QTP Tutorial
Nikolay Advolodkin is infinitely passionate about three things: self-improvement, IT & computers, and making an impact. He teaches people the art of automated software testing and quality assurance worldwide. His goal is to help develop higher quality software in a more efficient manner. Nikolay is the creative mind and co-owner behind QTP Tutorial. Follow Nikolay on his Twitter page for all of the latest updates: @Nikolay_A00

Pin It on Pinterest

Clef two-factor authentication