0

I am using ANT deployment tool on Salesforce to run specific test classes. For an example, my build.xml looks like below.

<target name="deployCode">
    <sf:deploy username="${sf.username}" password="${sf.password}" 
           sessionId="${sf.sessionId}" serverurl="${sf.serverurl}"
           deployroot="codepkg" testLevel="RunSpecifiedTests">
        <runTest>TEST_AccountController</runTest>
    </sf:deploy>
</target>

My package.xml Looks like this

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
    <types>
        <members>AccountController</members>
        <members>TEST_AccountController</members>
        <name>ApexClass</name>
    </types>
    <version>45.0</version>
</Package>

But in my classes folder, there are some other Apex classes that are not specified in package.xml

My problem is, for those classes, I get the error Error: Not in package.xml

If I put <members>*</members> in package.xml then it run all the test classes other than the one that I specified in the build.xml

How can I run only specific test classes while there are many other Apex classes in the classes folder?

3

This isn't something you can do in Ant directly. You would need to build a temp folder, copy the files you need to this folder, and deploy that instead. This was a limitation of the older metadata api (mdapi for short). Use Salesforce DX if you want this type of flexibility:

sfdx force:source:deploy -p force-app/main/default/classes/AccountController.cls -l RunSpecifiedTests -r TEST_AccountController -w 10
| improve this answer | |
2

The classes in classes folder and in package.xml should be an exact match. Else deployment will fail. The deployment parameters like which test classes to run can be specified in ant build file like.

<target name="deployCode">
    <sf:deploy username="${sf.username}" password="${sf.password}" 
           sessionId="${sf.sessionId}" serverurl="${sf.serverurl}"
           deployroot="codepkg" testLevel="RunSpecifiedTests">
        <runTest>TestClass1</runTest>
        <runTest>TestClass2</runTest>
        <runTest>TestClass3</runTest>
    </sf:deploy>
</target>

src: https://developer.salesforce.com/docs/atlas.en-us.daas.meta/daas/forcemigrationtool_deploy_run_specific_tests.htm?search_text=test

| improve this answer | |
  • Thanks, In my actual scenario, the source folder is a GIT repo. That's why I want to run test / deploy specific classes while there are some other classes in the folder. So, seems its not possible. – Dinesh Wickramasinghe May 11 at 10:20
  • manually create package.xml based on git commit files? – Pranay Jaiswal May 11 at 10:20
  • You can see the manually created package.xml (specifying only the classes I need) file in my question. Then I get "Not in package.xml" error for the other classes in the folder – Dinesh Wickramasinghe May 11 at 10:24
0

In the ANT deployment folder.. keep only those classes which u want to deploy and in buid.xml file add those test classes which u want to run. like below i have

<target name="deployCodeCheckOnly">
  <sf:deploy username="${sf.username}" password="${sf.password}" sessionId="${sf.sessionId}" serverurl="${sf.serverurl}" maxPoll="${sf.maxPoll}" deployRoot="retrieveUnpackaged" checkOnly="true" testLevel="RunSpecifiedTests">
    <runTest>ContactTrigger_Test</runTest>
  </sf:deploy>
</target>
| improve this answer | |
  • In my actual scenario, the source folder is a GIT repo. That's why I want to run test for specific classes only, while there are some other apex classes in the folder. – Dinesh Wickramasinghe May 11 at 10:26
  • 1
    in tht case..u can create a new branch .. run specific test classes for specific classes...then when successful u can merge those changes to main branch – hellraizer May 11 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.