[SalesForce] Run Specific Test Classes Ant Deployment Tool

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?

Best Answer

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
Related Topic