Idan Bliech
3 min readFeb 27, 2023

tl;dr

sfdx force:apex:test:run --codecoverage --detailedcoverage --resultformat junit --wait 30 > tests.xml

npx xunit-viewer -r tests.xml

Apex Tests

Salesforce Apex is a powerful programming language that allows developers to create custom business logic, triggers, and automated workflows (using code) on the Salesforce platform. However, with great power comes great responsibility, and it’s important to thoroughly test your Apex code to ensure that it’s functioning as intended.

Salesforce provides a robust testing framework that allows developers to write automated tests for their Apex code.

These tests can be executed using the force:apex:test:run command in the Salesforce CLI, which provides a convenient way to run tests from the command line and get real-time feedback on the results.

Here’s a step-by-step guide to using force:apex:test:run to test your Apex code:

  1. Open your terminal or command prompt and navigate to your Salesforce project directory.
  2. Use the sfdx force:apex:test:run command to execute your test classes. You can specify the test class or test classes you want to run using the -n or -s flags.
  3. Once the tests have completed, you’ll see a summary of the results in your terminal window. You can also view the results in Salesforce by navigating to the Tests tab in the Developer Console.

JUnit Format

Exporting test results into JUnit format is a widely used practice in the software development industry. JUnit is a popular testing framework and it provides a standardized format for test results that can be easily read and analyzed by other tools and systems. By exporting test results in JUnit format, developers can integrate their test results with other tools such as continuous integration servers or reporting tools, allowing for more efficient and streamlined development processes.

Salesforce’s sfdx provide the ability to export test results in JUnit format, enabling developers to integrate their test results with other tools and systems in their development pipeline.

Using SFDX and Junit together can create a great, easy-to-present HTML report that’ll give you quick results for your team.

Xunit Viewer

Xunit Viewer is an npm module that provides a simple and effective way to create HTML reports from JUnit test results. This module takes JUnit XML files as input and generates a user-friendly HTML report that is easy to read and understand. The report includes detailed information about the tests that were run, including the test suite, test case names, test status, and duration.

Just run this in your terminal:

sfdx force:apex:test:run --codecoverage --detailedcoverage --resultformat junit --wait 30 > tests.xml

npx xunit-viewer -r tests.xml

Once the report is generated, you can view it in any web browser. The report includes a summary of the test results, as well as a detailed breakdown of each test case. You can also filter the results based on test status, test suite, or test case name.

🚩🚩 My Latest App is now on the AppExchange
Viewer for Document Generator. This app is based on word template, it includes unlimited users and unlimited templates and unlimited automation. Here is the link: https://4winds.link/VIewerLP