Interested in a
Personal Demo ?


Name* :
Please Enter your Name
Company E–Mail* :
Please Enter a Valid Email

Testimonials


We need to maintain legacy applications that were not built to facilitate unit testing.  Without Typemock we would be completely unable to effectively test these applications.  Typemock boosts productivity because you do not have to explicitly code from the ground up with testing in mind, which can be difficult to handle.  Typenock has been able to cover up the coupling and dependency sins that we all have leaked into our applications from time to time.  
Edmund D. Gorski, President, CodePoets, Inc.
Success stories

PurchaseOptions


US Toll Free
877–634–0165
Outside US
+972–3–6815177
Get your printable quote
Buy online starting from $249

 

Creating a TestLint Rule
  1. Create a class library project
  2. Add references to
    1. Typemock.Lint.SDK.dll
    2. Typemock.CodeReview.Common.dll
    3. ICSharpCode.NRefactory.dll
  3. which can be found at C:Users[your user name]AppDataLocalMicrosoftVisualStudio10.0ExtensionsTypemockTypemock Test Lint1.1200.201
  4. Create a new class, and add the following using: using Typemock.Lint.SDK;
  5. Inherit from BaseTestReviewer
    public class MyRule:BaseTestReviewer
        {
        }
  6. Let's say we're trying to create a rule that flags "IF" logic inside a unit test as problematic. We need to override the base method that "visits" "IF" logic:
    public override object VisitIfElseStatement(
    IfElseStatement ifElseStatement, object data)
            {
                return base.VisitIfElseStatement(ifElseStatement, data);
            }
  7. To flag the location, use the ExpressionHelper class to make a ReviewInfo object form the current Code Element, and add it to the current rule's Reviews collection. You should also provide the url that will be used when the user clicks "more information" (just use http:/typemock.com as the default. Also provide the title and text to describe that rule.
    public override object VisitIfElseStatement(
    IfElseStatement ifElseStatement, object data)
            {
                ReviewInfo info = 
                    ExpressionHelpers.MakeReviewFromExpression(this, ifElseStatement,
                                      ruleUrl: "http:/MyUrl.com",
                                      text: "You can't have IFs inside tests",
                                      title: "IF discovered");
                Reviews.Add(info);
                return base.VisitIfElseStatement(ifElseStatement, data);
            }
  8. Now, make sure you are only reviewing tests:
    public override object VisitIfElseStatement(
    IfElseStatement ifElseStatement, object data)
            {
                if (false==IsInsideTestMethod)
                {
                    return base.VisitIfElseStatement(ifElseStatement, data);
                }
                
    /...
            }
  9. Compile the project. Put the compiled dll under C:Users[your user name]AppDataLocalMicrosoftVisualStudio10.0ExtensionsTypemockTypemock Test Lint1.1200.201Extensions
  10. You don't need to close visual studio. Just open a new source file and the new rule will be loaded.
 
How to write unit tests for your rules:
  1. Create a test project
  2. Add the three references as shown in the first section, item #2
  3. Write a test that asks the rule to check a specific file code and then checks how many reviews it returns (I it’s flags, it should have one review or more)
[Test]
        public void MyRule_CodeWithIF_Flagged()
        {
            string code = @"
                        namespace Something
                        {
                            [TestFixtures]
                            public class SomeClass
                            {
                                [Test]
                                public void SomeTestToReview()
                                {
                                    if(something)
                                    {
                                        /do something
                                    }
                                }
                            }
                        }
";
            MyRule rule = new MyRule();    
            rule.ReviewSingleFileContent(code);

            Assert.AreEqual(1, rule.Reviews.Count);
        }