Gå till huvudinnehållet Gå till huvudmenyn

EPiServer content types hygiene unit tests

Av Ove Lartelius Lästid: 4 minuter

Some developers are lazy or just not paying attention of the attributes set on content types and properties. I believe that developers should set the correct information as soon as possible and not wait until the end of the project go through and set data to the needed attributes for content types and properties.

If you do it during the development of a feature, I think you have the best mindset to describe what the content type/properties are for.

I also want to raise the standard and hygiene on the deliverables, so I created these unit tests that uses reflection go through all content types and their properties and look on what attributes are set and that they are correct. The tests are written to be reusable so that you have the option to turn off a test without deleting or uncomment the code/test for any specific project.

Features

Content types

Check that developers have set a "Guid" attribute.
Check that developers have not set the same Guid value on different content types.
Check that developers have set a "DisplayName" attribute.
Check that developers have set a "Description" attribute.
Check that developers have set a "GroupName" attribute.
Check that developers have set a "ImageUrl" attribute.
Check that developers have set a "Order" attribute.
Check that developers have not set the same Order value on different content types.

Content type properties

Check that developers have set a "Name" attribute.
Check that developers have set a "Description" attribute.
Check that developers have set a "ShortName" attribute.
Check that developers have set a "GroupName" attribute.
Check that developers have set a "Order" attribute.
Check that developers have not set the same Order value on different properties in the same content type.
Check that developers have set a "Prompt" attribute on string properties.

How to get started

  1. Clone the project from GitHub. https://github.com/ovelartelius/EpiserverHygieneDemo/
  2. In your unittest project/structure. Create a new class with the name “ContentTypesHygieneTest.cs”.
  3. Copy the code (from your clone or directly from GitHub) and paste into the new created class.
  4. Change the namespace in the copied code to fit your location in your project.
    Development spec
  5. Set the correct namespace to your website assembly. You simply check the website project settings and use “[Assembly name].Global”.
    Development spec
    Development spec
  6. Set which tests you want to run on your project. The properties that are set to true will be run. We strongly recommend that you run them all. Always!
  7. Now you should be ready to compile and run the tests.

How to set tests on/off

Some projects use special custom functionality to generate translation for names, descriptions or other attributes. So, I added some logic that could turn on and off tests in the class. The test is implemented on a template project that will be used when new projects are created. Then it is nice that each project, can decide them self, what kind of tests that should want to run.
In the beginning of the "ContentTypesHygieneTests" class there are a couple of properties that you can set to true/false. The list below describes in more detail what the test does.

The following properties can be turned on/off:
Development spec

What does the tests do?

Check_ContentTypeGuid

If true: Two test will be executed.
1. Checks that all content types has the "GUID" attribute set. 
Development spec
2. Checks if there is any duplicate GUID specified on content types.
Development spec
If any content types missing the attribute, the test will fail, and report which content types that does not contain the "GUID" attribute.
Development spec
If any duplicate GUID´s will be found, the test will fail, and report which content types that are using the same GUID.
Development spec

Check_ContentTypeDisplayName

If true: Checks that all content types has the "DisplayName" attribute set.
Development spec
If any content types missing the attribute or are less then 3 characters, the test will fail, and report which content types that does not contain the "DisplayName" attribute.
Development spec

Check_ContentTypeDescription

If true: Check if all content types has the "Description" attribute set.
Development spec
If any content types missing the attribute or are less then 3 characters, the test will fail, and report which content types that does not contain the "Description" attribute.
Development spec

Check_ContentTypeGroupName

If true: Check if all content types has the "GroupName" attribute set.
Development spec
If any content types missing the attribute, the test will fail, and report which content types that does not contain the "GroupName" attribute.
Development spec

Check_ContentTypeImageUrl

If true: Check if all content types has the "ImageUrl" attribute set.
Development spec
If any content types missing the attribute, the test will fail, and report which content types that does not contain the "ImageUrl" attribute.
Development spec

Check_ContentTypeOrder

If true: Check if all content types has the "Order" attribute set.
Development spec
If any content types missing the attribute, the test will fail, and report which content types that does not contain the "Order" attribute.
Development spec

Check_ContentTypeDuplicateOrder

If true: Check if all content types has unique order index.
[BILD]
If any content types have the same order index, the test will fail, and report which content types that use the same order index number.
Development spec

Check_PropertyName

If true: Check if all properties has the "Name" attribute set.
Development spec
If any properties missing the attribute or are less then 2 characters, the test will fail, and report which [ContentType].[Property] that does not contain the "Name" attribute.
Development spec

Check_PropertiesDescription

If true: Check if all properties has the "Description" attribute set.
Development spec
If any properties missing the attribute or are less then 2 characters, the test will fail, and report which [ContentType].[Property] that does not contain the "Description" attribute.
Development spec

Check_PropertiesOrder

If true: Check if all properties has the "Order" attribute set.
Development spec
If any properties missing the attribute, the test will fail, and report which [ContentType].[Property] that does not contain the "Order" attribute.
Development spec

Check_PropertiesShortName

If true: Check if all properties has the "ShortName" attribute set.
Development spec
If any properties missing the attribute or are less then 2 characters, the test will fail, and report which [ContentType].[Property] that does not contain the "ShortName" attribute.
Development spec

Check_PropertiesDuplicateOrder

If true: Check if all properties with in a content type has unique order index.
Development spec
If any properties has the same order index, the test will fail, and report which [ContentType].[Property] that use the same order index number.
Development spec

Check_PropertiesGroupName

If true: Check if all properties has the "GroupName" attribute set.
Development spec
If any properties missing the attribute or are less then 2 characters, the test will fail, and report which [ContentType].[Property] that does not contain the "GroupName" attribute.
Development spec

Check_PropertiesStringPrompt

If true: Check if all string properties has the "Prompt" attribute set.
Development spec
If any string properties missing the attribute or are less then 2 characters, the test will fail, and report which [ContentType].[Property] that does not contain the "Prompt" attribute.
Development spec

Vi vill gärna höra vad du tycker om inlägget

Ove Lartelius
Ove Lartelius

Backend-utvecklare | OMVP

Läs alla blogginlägg av Ove Lartelius