By | 07 February 2012

The Importance of Having a Unit Test Page

When coding PeopleCode, you can save yourself hours of time and mountains of frustration by having a very simple unit test page. You can use this page to try small pieces of PeopleCode in isolation of your component or module. I always have a simple unit test page setup in a development database ready to go and this has personally saved me hours of development and testing time because I can isolate and test a small piece of code that I know is having an issue.

Let’s say that you are working on a large module and you need to quickly test a supporting function you coded but testing the whole module requires a lot of setup and tear down. Maybe you are working with some new piece of code that you can’t quite figure out and you want to test just a few lines.

Here is what my test page looks like. I keep it on its own menu, record and component so it can be imported without any implications to other objects.

The strategy is to just put in some fieldchange code and have your outputs print out to an html area on a page.

Here is what the record looks like.

Here is what the page looks like.

Here is what the component looks like.

Here is what a unit test would look like. In this case, I am just doing a quick test to see what the %role variable returns. I always have a simple “a” function to append some string to the html area on the page. I just use this so the code is small.

Function a(&v As any)
  Local Field &fld;
  &fld = GetLevel0()(1).CRM_UNIT_TEST.HTMLAREA;
  &fld.Value = &fld.Value | "<br /> " | &v;

Function UnitTest()
  a("starting unit test <hr />");
  Local array of string &roles;
  &roles = %Roles;
  a(&roles.Join(" <hr />"));


Here is what the PIA page looks like before we press the button.

Here is what the PIA page looks like after we press the button.

Do you want to become a CI Expert?

If you want to learn more about developing using Component Interface then check out our CI Training Video.