top of page

Salesforce Test Automation with Python

Salesforce, a popular CRM system, helps sales, marketing, customer service, finance and IT teams communicate with and gather customer information. Companies rely on Salesforce to complete a wide variety of critical tasks. So if a core system like Salesforce doesn't work as intended, sales and customer success fail, potentially a huge loss in revenue, putting the whole organization at risk.


Automating Salesforce testing end to end is essential for existing capabilities to function as intended for the incremental & continuous delivery(s).


The goal is to ensure that Salesforce integration with enterprise services is capable of communicating and storing the expected data.


Best Practice


We are not testing Salesforce, it's a pretty stable and well known product. Instead, our focus is on testing data stored by the enterprise services in Salesforce and then used for marketing or customer campaigns.


It is advisable not to fall into a scenario to automate Salesforce UI due to its dynamic nature, there are frequent releases/changes on the Salesforce UI which will lead to flaky tests. It is therefore recommended that Salesforce Rest Clients be used to automate its testing.


Test Pyramid


The Test Automation Pyramid represents the different types of automation tests planned to achieve Salesforce tests and their test coverage as part of the codebase. This is about getting immediate feedback as code changes do not break the existing feature. There are three separate sections to the test pyramid. Unit tests are at the lowest level, integration tests are the intermediate level and end-to-end tests are the upper level.


End to End Tests


The purpose of end-to-end testing is to test the real user journey (human centric) and validate the system under test and its actual components for integration and data integrity.


Tests the way a user engages with the product. At this level of this pyramid, end-to-end tests are 5 - 10% of the entire test coverage.



Simple Salesforce


It's a rest API client which allows forming connection & query Salesforce for the development and testing purposes, especially automating end-to-end tests.


Connection with Salesforce


from simple_salesforce import Salesforce
  
salesforce_client = Salesforce(username='test_username',
                    password='test_password',
                    domain='test_domain',
                    security_token='test_security',
                    version='test_version')

Create a Customer Contact


salesforce_client.Contact.create({'FirstName':'George','LastName':'Smith','Email':'example@example.com'})

This will output a result as {u'errors':[],u'id':u'001e0000001TuSFZZ1',u'success':True}


Delete Customer Contact

This could be useful as part of tear down process, cleaning up the test data creation after the tests are performed.


salesforce_client.Contact.delete('001e0000001TuSFZZ1')

Query Salesforce


It's also possible to write select queries in Salesforce Object Query Language (SOQL) and search queries in Salesforce Object Search Language (SOSL). It could be useful for the assertion purposes.


SOQL Query


salesforce_client.query("SELECT Id, Email FROM Contact WHERE Contact_id = '001e0000001TuSFZZ1'")

SOSL Query


sf.search("FIND {George}")
salesforce_client.quick_search("George")

Integration Tests


It is good to automate the services with simulated Salesforce, this way we could automate various scenarios from different aspects to discover the problems earlier.


Even if one part is mocked, i.e. Salesforce, these tests are conducted using the services that generate the data such as customer, marketing or sales


Simple Mockforce


simple-mockforce , a companion package for simple-salesforce that enables testing for code that interacts with Salesforce’s REST API.


import os

from simple_mockforce import mock_salesforce

from simple_salesforce import Salesforce

@mock_salesforce
def test_api_mock():salesforce = Salesforce(username=os.getenv["SFDC_USERNAME"],
password=os.getenv["SFDC_PASSWORD"],
security_token=os.getenv["SFDC_SECURITY_TOKEN"])

response = salesforce.Account.create({"Name": "Test Account"})

account_id = response["id"]

account = salesforce.Account.get(account_id)

assert account["Name"] == "Test Account"

In my next blog, I will be sharing End to End test framework using pytest-bdd to automate Salesforce test scenarios!


References




234 views0 comments

Comments


bottom of page