Interested in a
Personal Demo ?


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

Testimonials


"I have been using Typemock Isolator for quite some time and I would like to thank the Typemock team for such a great product which I believe is the only product that makes Unit Testing really possible"
Rahul Das, Technical Lead, Bank Societe Generale
Success stories

PurchaseOptions


US Toll Free
866–895–4680
Outside US
+44–203–150–0293
Get your printable quote
Buy online

 

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);
        }