+ Start a Discussion
Nikki Phillips 8Nikki Phillips 8 

.Net application to run Apex TC through tfs build system

I am currently automating a salesforce application using selenium and apex TC.  We use the salesforce Soap api and enterprise wsdl for our DML and SOQL updates, queries, etc. We are also using the soap api to run the apex tests. We run all of our selenium tests through tfs build using the microsoft build system.  The reporting reports on each test method if it to fail or pass.  I am trying to figure out how to run each apex test method instead of running the entire class.

The code below is one selenium method that runs the entire apex TC class with many methods.  I want to be able to do a 1 to 1 and have a selenium method call each apex test method however I am not finding away to do this.

The Selenium test method:

        public void TestFromEventDetailsSprint181()
            //This line of code logs into the dev org as defined in App.config and runs the specified apex test class.
            //   Use intellisense to see the available properties returned in testResults. This can be used to assert against for reporting back to TFS. 

            //CommonFunctionLibrary.SfApex.RunTestsResult testResults = Helper.RunSalesforceApexTestClass("TestForecastedRevWithoutECUpts_Test", null);
            CommonFunctionLibrary.SfApex.RunTestsResult testResults = Helper.RunSalesforceApexTestClass("somenamespace.someclass", null);
            //Some notable data available in the returned results to assert against:
            //   numTestRuns = # of testmethods executed in the apex test class
            //   numFailures = # of failed test methods
            //   failures = array of failure structures, each contains the methodName and message (see below)
            //   successes = array of success structures

            string failureMessage = "";
            if (testResults.numFailures > 0)
                foreach (var failure in testResults.failures)
                    failureMessage += "Test failure encountered in method: " + failure.methodName + ", error: " + failure.message + ".\n";

            //For demo purposes this assert assumes at least 1 testmethod will fail, as designed in the example TestClassTemplate run above.
            Assert.AreEqual(0, testResults.numFailures, failureMessage);

The soap api called method to run the apex test class:

    public static RunTestsResult RunSalesforceApexTestClass(string testClassName, string testClassNamespace)
            bool isSandbox = !AppSettings["EnvironmentType"].Equals("developer");

            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

            //Log into SFDC. Retrieve the Session ID and instance URL.
            SoapClient soapClient = new SoapClient((!isSandbox ? "Sfdeveloper" : "Sfsandbox"));

            SfPartner.LoginResult loginResult = soapClient.login(null, null, AppSettings["Username"], AppSettings["Password"]);

            SfApex.SessionHeader apexSessionHeader = new SfApex.SessionHeader();
            apexSessionHeader.sessionId = loginResult.sessionId;

            RunTestsRequest runTestsRequest = new RunTestsRequest();
            runTestsRequest.classes = new[] { testClassName };
            runTestsRequest.@namespace = (string.IsNullOrEmpty(testClassNamespace) ? AppSettings["OrgNameSpace"] : testClassNamespace);

            ApexService apexService = new ApexService();
            apexService.Url = loginResult.serverUrl.Replace("/u/", "/s/");   //The instance URL has a path change to call the SOAP service
            apexService.SessionHeaderValue = apexSessionHeader;

            RunTestsResult ret = apexService.runTests(runTestsRequest);

            return ret;


Daniel BallingerDaniel Ballinger
Linking to answer for the same question on the Salesforce StackExchange - .Net application to run Apex TC through tfs build system (http://salesforce.stackexchange.com/a/167731/102)

The short version of that answer is that you can use the REST API to run specific test methods individually.
Brian LangeBrian Lange
Maybe you could also use a little bit of System.Reflection on the API and call the APEX test methods dynamically.
string strFullyQualifiedName = "Namespace.Class";
string strMethodName = "runTest1";
Type type = Type.GetType(strFullyQualifiedName);
object instance = Activator.CreateInstance(type);
System.Reflection.MethodInfo method = type.GetMethod(strMethodName);
method.Invoke(strMethodName, null); //Run Test Method