January 14, 2009

Create Test Case from Requisite Pro

Helix ALM
This is an example of how you can create a Requisite Pro add-in to create TestTrack test cases. This example creates a very basic test case based on the selected requirement in the Requisite Pro explorer.

Field Mapping

In this example the mapping is very simple, only one field is mapped: Test Case Summary: Requirement Prefix, Requirement ID, and Requirement Text. Type: Set to Requirements.

Sample Code

Following is the source code of my add-in. I wrote this in C# using Visual Studio 2005. I added to my references the nescessary Requiste Pro libraries (mainly ReqPro40.dll and RQGUIAPP.dll - this comes as a tlb file but I had to convert it to a .dll in order to reference it). Keep in mind that I have hard coded the TestTrack information, like the SOAP cgi address, the project name, etc...
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Reflection;
using System.Text;
using ReqPro40;
using RQGUIAPP;

namespace RPCreateTestCase
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Gathering information about selected requirement...");
            ReqPro40.Requirement rpRequirement = GetCurrentReq();
           // return requi;
            Console.WriteLine("Attempting to create test case...");
            string status = CreateTestCase(rpRequirement);
            if (status == "")
            {
                MessageBox.Show("TestCase created without errors");
            }
            else
            {
                MessageBox.Show("An error occurred while trying to create the test case.n" + status);
            }
        }
        public static ReqPro40.Requirement GetCurrentReq()
        {
            Type objAddType;
            object objAdd;

            objAddType = Type.GetTypeFromProgID("ReqPro40.GUIApp");
            //get interface

            objAdd = Activator.CreateInstance(objAddType);
            //create instance
            object[] myArguments = null;

            string invMember = "CurrentProject";

            System.Reflection.BindingFlags bindingFlags = BindingFlags.InvokeMethod;

            Binder binder = null;

            object invResult = objAddType.InvokeMember(invMember, bindingFlags, binder, objAdd, myArguments);
            ReqPro40.Project rqProject = (ReqPro40.Project)invResult;

            invMember = "SelectedExplorerKeys";
            invResult = objAddType.InvokeMember(invMember, bindingFlags, binder, objAdd, myArguments);

            String[] reqKeys;
            reqKeys = (String[])invResult;
            string reqID = reqKeys[0];
            reqID = reqID.Substring(reqID.IndexOf("-") + 1);
            Requirement requi = null;
            try
            {
                requi = rqProject.GetRequirement(reqID, enumRequirementLookups.eReqLookup_Key, enumRequirementsWeights.eReqWeight_Attrs, enumRequirementFlags.eReqFlag_RetainHierarchy);

            }
            catch (Exception fcc)
            {
                MessageBox.Show(fcc.Message);
                MessageBox.Show(fcc.InnerException.Message);
            }

            Marshal.ReleaseComObject(objAdd);
            return requi;
        }
        public static string CreateTestCase(ReqPro40.Requirement requirement)
        {
            string oppResult = "";
            try
            {

                ttsoapcgi ttSoap = new ttsoapcgi();
                ttSoap.Url = "http://localhost/scripts/ttsoapcgi.exe";

                string ttUser = "Administrator";
                string ttPassword = "Password";
                string ttProjectName = "Sample Project";

                long ttCookie = 0;

                CProject ttProject = new CProject();
                CProject[] ttProjects = ttSoap.getProjectList(ttUser, ttPassword);
                for (int i = 0; i < ttProjects.Length; i++)
                {
                    if (ttProjects[i].database.name == ttProjectName)
                    {
                        ttProject = ttProjects[i];
                    }
                }
                try
                {
                    ttCookie = ttSoap.ProjectLogon(ttProject, ttUser, ttPassword);
                }
                catch (Exception fcc)
                {
                    MessageBox.Show("Error logging in to project.n" + fcc.Message);
                }
                if (ttCookie > 0)
                {
                    CTestCase ttTCase = new CTestCase();
                    ttTCase.summary = requirement.ReqType.ReqPrefix + requirement.get_Tag(enumTagFormat.eTagFormat_TagNumber) + " - " + requirement.Text;

                    ttTCase.type = "Requirements";
                    ttSoap.addTestCase(ttCookie, ttTCase);

                    ttSoap.DatabaseLogoff(ttCookie);

                }
            }
            catch (Exception fcc)
            {
                oppResult = fcc.Message;
            }
            return oppResult;
        }

    }
}

The menu file

The last piece is to create your .mnu file. This allows you to run the file from a menu option within Requisite Pro. This is an example of what your menu file may look like
Menu Tools
{
	Separator
	Option "Create TestTrack TCM Test Case"
	{
		Exec "C:ProjectsdevRPCreateTestCaseRPCreateTestCasebinDebugRPCreateTestCase.exe"
	}
}
And here is a screenshot of the menu option it creates:

Figure 1 - Menu option