Customize FxCop rules-detect TestCase in the Release version

Source: Internet
Author: User
When NUnit is used for development, TestCase is not economical. With TestCase, we can ensure the correctness of the written code :)

However, during the development process, TestCase is written while implementing functional code. If you do not pay attention to it, TestCase may be written in the final Release version... this should be avoided during program development as much as possible :)
There are several ways to solve this problem:
1. Use the # if debug xxx # endif tag. put all TestCase in this tag. in this way, NUNit can be used for testing when compiling in Debug mode. when compiled in Release mode, all the code will be ignored (equivalent to commented out ). in this way, no extra code will be generated :)
2. Put all TestCase in a separate project. In this way, the functional code cannot include the test code, which can also avoid the above problems ..

In a project, in order to perform the above checks on all the code, it is obviously unrealistic to manually check the code. now everyone is playing with a computer, so why not check it with a computer? :) oh, right, use it.. NET...

There is no such Rule in the Rules that comes with FxCop, so we have to customize a Rule :)

The Rule workflow is as follows:
1. obtain all the classes in this program.
2. For each Class, get its Attribute.
3. For each Attribute, determine whether it is NUnit. Framework. TestFixtureAttribute. If yes, an error is returned.

Note: Only the Release version can be used for real detection purposes.

By Customizing One of the examples of FxCop rules: AvoidICloneableImplementation, we can learn how to compile a basic FxCop Rule... but the content in the article cannot solve our problem .. let's Google it to see if there is any other information ..
Found this:
Writing custom rules [using introspection] for FxCop 1.312 (from: James Geurts)
FxCop Custom Rule-Introspection Engine 3.12

Note: The BaseIntrospectionRule class provides some overloaded versions of the Check () method. The available methods are:

Check (Member member)
-Useful for checking members of a class. (Methods, Properties, Events, Delegates, etc)
Check (Module module)
-Useful for checking assemblies.
Check (Parameter parameter)
-Checks all parameters
Check (Resource resource)
-Checks all resources (from. resx files)
Check (TypeNode type)
-Checks types
Check (string namespaceName, TypeNodeList types)
-Checks types

For the Rule we want to write now, we need to use 5th functions, namely: Check (TypeNode type)
By using the TypeNode Class, we can find that it has an Attribute such as Attributes, that is, all the Attributes of this Class can be obtained.
Its type is AttributeList ..
AttributeNode is stored in AttributeList, which is different from the Attribute in. net fx. That is, AttributeNode = Attribute cannot be directly determined.
Fortunately, TypeNode provides the FullName attribute, which stores the full name of the actual class.
That is, the FullName of TypeNode of the NUnit. Framework. TestFixtureAttribute class is "NUnit. Framework. TestFixtureAttribute"

Here, all the technical work is done. below is the code implementation
RuleInfo. xml <? Xml version = "1.0" encoding = "UTF-8"?>
<Rules FriendlyName = "Avoid NUnit TestCase in Release Version">
<Rule TypeName = "AvoidNUnitTestCase" Category = "Cnblogs. Rules" CheckId = "hbifts000001">
<Name> Avoid NUnit TestCases Implementation in Release Version </Name>
<Description> NUnit TestCases must never be supported ded in release versions. </Description>
<Url> http://www.cnblogs.com/hbifts </Url>
<Resolution> Type '{0}' Implements NUnit TestCases, which shoshould avoid in release version. You shoshould add # if degug xxx # endif. </Resolution>
<Email/>
<MessageLevel Certainty = "99"> Error </MessageLevel>
<FixCategories> Breaking </FixCategories>
<Owner/>
</Rule>
</Rules>

AvoidNUnitTestCase. cs implements public class AvoidNUnitTestCase: BaseIntrospectionRule
{
Public AvoidNUnitTestCase (): base ("AvoidNUnitTestCase", "Cnblogs. Rules. RuleInfo", typeof (AvoidNUnitTestCase). Assembly)
{
}

Public override ProblemCollection Check (TypeNode type)
{
If (DoesImplementConditionAttribute (type. Attributes )){
Resolution resolution = GetResolution (RuleUtilities. Format (type ));
Problem problem = new Problem (resolution, type );
Problems. Add (problem );
Return Problems;
}
Return null;
}

Private bool DoesImplementConditionAttribute (AttributeList Lists)
{
Log. WriteFile (Lists. Length. ToString ());
For (int I = 0; I <Lists. Length; I ++ ){
If (Lists [I]. Type. FullName. Equals ("NUnit. Framework. TestFixtureAttribute ")){
Return true;
}
}
Return false;
}
}

Compile it into a dll .. add it to the Rules of FxCop, so that we can easily check whether the Release version exists after daily Build :)

All Happy ~ :)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.