let's talk about Testing - Trailblazer Community

yosraBenHmida 95 views 23 slides Oct 04, 2024
Slide 1
Slide 1 of 23
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23

About This Presentation

let's talk about Testing - Trailblazer Community


Slide Content

Architect Group
Paris, France
Salesforce
Let’s talk about Salesforce Testing:
Best Practices & Practical Approaches
XX
Texeï
24 Septembre à 18h45
Emmanuel Mery
Senior Program Architect,
Salesforce

Why executing Tests
Session agenda:
What are the different test types
How to execute testing
What needs to be tested
When shall you start thinking about Tests
Where to Test

Why executing Tests
●Identify defects
●Ensure functionality
●Enhance application quality
●Support refactoring
●Prevent issues and risks
●Maintain software integrity
●Cost-effectiveness

Overall, executing testing in application development is crucial to ensure the quality,
functionality, and reliability of the application. It helps in identifying and fixing issues,
enhancing user satisfaction, reducing risks, and ensuring a smooth user experience.

What are the different test types
●Component Interaction
●Test Scope
●Dependencies
●Testing Levels
●Order of Execution
●Test Environment
The primary objective is to
uncover defects or issues
that arise when different
components are integrated
and work together.
By validating the integration
points, interactions, and
data exchanges, integration
tests help ensure that the
system as a whole behaves
correctly and meets the
desired functionality and
requirements.
●Granularity
●Independence
●Isolation
●Determinism
●Oriented
The primary goal is to catch
and fix defects in the early
stages of development,
allowing technical team to
iteratively build and refactor
configuration and code with
confidence.
By testing units in isolation,
team can quickly identify
issues and ensure that each
unit behaves as expected,
leading to more robust and
maintainable code.
●End-to-End Testing
●Functional Testing
●Non-Functional Testing
●User Interface Testing
●Compatibility and Integration
Testing
●Performance and Load Testing
●Regression Testing
The main goal is to ensure that
the software system as a whole
meets the desired quality
standards and behaves as
expected in a real-world
environment.
By assessing the complete
system, system tests help identify
and address any issues or gaps in
functionality, performance, or
usability, providing confidence in
the system's readiness for
deployment.

●Scope
●Methodology
●Vulnerability Assessment
●Exploitation and Analysis
●Reporting
Penetration testing plays a
critical role in proactive
security by identifying
vulnerabilities before they
are exploited by malicious
actors.
It helps organizations identify
weaknesses, prioritize
security efforts, and
implement appropriate
security measures to protect
their systems and data from
potential threats.
unit test integration tests system tests penetration tests

What needs to be
tested
Rules
Flows
Profiles
Permission Sets
Apex
What else?

How to execute testing: Rules
DmlException expectedException;
Test.startTest();
try
{
// save record which should be
prevented from saving
}
catch (DmlException d)
{
expectedException = d;
}
Test.stopTest();

assert.areNotEqual(null, expectedException,
'The record should be validated');

DmlException unexpectedException;
Test.startTest();
Try
{
// save record which should save
successfully
}
catch (DmlException d)
{
unexpectedException = d;
}
Test.stopTest();

assert.areEqual(null, unexpectedException,
'The record should be saved');
Apex Negative testing example Apex Positive testing example
Manual or Apex Testing

Flow can be triggered or launch from
apex: Launching a flow from apex
sfdx force:data:soql:query --query
"
SELECT Id, ApexTestClassId, TestMethodName,
FlowVersionId, NumElementsCovered,
NumElementsNotCovered
FROM FlowTestCoverage
WHERE FlowVersion.Definition.DeveloperName =
'Flow_Name' AND TestMethodName
='test_Method_Name_in_Class'
"
--usetoolingapi
○From Setup, in the Quick Find box, enter
Flows, and then select Flows.
○Open the relevant version of the flow.
○Save your changes.
○Unsaved changes aren't executed when
you test the flow.
○Click View Tests, and then click Create.
When you’re confident that your flow is
working as expected, activate the version
that you tested and distribute the flow.
How to execute testing: Flows
Manual Testing, Test Flow feature, Apex Testing

Steps to create a flow testing
Calculate test coverage

Test flow feature example: the screenflow New Contact
Screen :
●Contact First Name
●Contact Last Name
●Contact Account
●New or Existing ?
●Set local contact variable
Search contact
●First Name
●Last Name
Create or update
●New or Existing ?
●Found or not
Set local Variable with account id
Update contact
Create Contact
How to execute testing: Flows

Matching record
Toggle
Doesn’t Exist Exist
Deselected A contact is createdA contact is created
Selected A contact is createdA contact is updated
Test definition
Test execution : use debug
How to execute testing: Flows
Test flow feature example : Testing

What needs to be Tested and how to execute testing
Test a trigger flow : flow example

What needs to be Tested and how to execute testing
Test a trigger flow : Define a test in 3 or 4 steps
When executing the test
Values to use for testing
Define assertions to test

Id p = [select id from profile where name=' XXXX'].id;
Account ac = new Account(name ='Test') ;
insert ac;
Contact con = new Contact(LastName ='testCon',AccountId = ac.Id);
insert con;
User user = new User(alias = 'test123', email='[email protected]',
emailencodingkey='UTF-8', lastname='Testing',
languagelocalekey='en_US',
localesidkey='en_US',
profileid = p,
country='United States',IsActive =true,
ContactId = con.Id,
timezonesidkey='America/Los_Angeles', username='[email protected]');
insert user;
system.runAs(user) {
// statements to be executed by this test user.
}
How to execute testing: Profiles
Manual or inside Apex using runAs

PermissionSet ps = [SELECT Id FROM PermissionSet
WHERE Name = '<required permission set name>'];
insert new PermissionSetAssignment(AssigneeId = opUser.id, PermissionSetId = ps.Id);


SELECT Id, PermissionSetId, PermissionSet.Name, PermissionSet.ProfileId,
PermissionSet.Profile.Name, AssigneeId, Assignee.Name
FROM PermissionSetAssignment
WHERE Assignee.Name = 'John Doe'


SELECT Id, PermissionSetId, PermissionSet.Name, PermissionSet.ProfileId,
PermissionSet.Profile.Name, AssigneeId, Assignee.Name
FROM PermissionSetAssignment WHERE
PermissionSet.Name = 'Custom PermissionSet Name'
How to execute testing: Permission Sets
Manual or inside Apex

●Cover as many lines of code as possible. Before you can deploy Apex or package it
for the AppExchange, the following must be true.
●If code uses conditional logic (including ternary operators), execute each branch.
●Make calls to methods using both valid and invalid inputs.
●Complete successfully without throwing any exceptions, unless those errors are
expected and caught in a try…catch block.
●Always handle all exceptions that are caught, instead of merely catching the
exceptions.
●Use assert methods to prove that code behaves properly.
●Use the runAs method to test your application in different user contexts.
●Exercise bulk trigger functionality—use at least 200 records in your tests.
●Use the ORDER BY keywords to ensure that the records are returned in the
expected order.
How to execute testing: Apex

Not assume that
record IDs are in
sequential order
Set Up Test Data Document your test
Execute individual
tests
When writing Apex test classes
Record IDs are not
created in ascending
order unless you insert
multiple records with the
same request.
For example, if you
create an account A,
and receive the ID
001D000000IEEmT,
then create account B,
the ID of account B may,
or may not be
sequentially higher.
Create the necessary
data in test classes, so
the tests don’t have to
rely on data in a
particular organization.
Create all test data
before calling the
Test.startTest
method.
Since tests don't commit,
you don't have to delete
any data.
Write comments stating
not only what is
supposed to be tested,
but the assumptions the
tester made about the
data, the expected
outcome, and so on.
Test the classes in your
application individually.
Never test your entire
application in a single
test.

Best Practices for Apex Parallel Test Execution
Tests that are started from the Salesforce user interface (including the Developer Console)
run in parallel.
Parallel test execution can speed up test run time. Sometimes, parallel test execution results in
data contention issues, and you can turn off parallel execution in those cases.
In particular, data contention issues and UNABLE_TO_LOCK_ROW errors can occur in the following
cases:
●When tests update the same records at the same time. Updating the same records
typically occurs when tests don’t create their own data and turn off data isolation to access
the organization’s data.
●When a deadlock occurs in tests that are running in parallel and that try to create records
with duplicate index field values. A deadlock occurs when two running tests are waiting for
each other to roll back data. Such a wait can happen if two tests insert records with the
same unique index field values in different orders.

Is it too early to start
thinking about testing?

Test Driven Development Methodology
1.Write a user story and its
acceptance criteria
2.Write a Test
3.Run the Test
4.Write Minimal Code/ execute the
smallest configuration
5.Run All Tests
6.Refactor Code / Configuration
7.Repeat

Benefits of Test Driven Development
1. Increased Test Coverage
2. Improved Code Quality
3. Faster Debugging
4. Faster Feedback Loop
5. Design Focus

Where to test ?
Unit
(dev)

Integration
(partial)
System
(full)
Penetration
(partial)
Unit testing are executed by the
administrator or developer. It aims to
double-check the new feature and can
be done in the environment where the
feature has been created (Dev Sandbox)
Integration testing aims to double
how the new features behave with other
systems component. It requires
connectivity to other systems. These
“external“ systems may require to access
or create data. This is why even if some
testing can be done in a dev sandbox,
using a partial sandbox makes sense.
Penetration testing must be
executed on an environment as close
as possible from production. It means
exposition to the outside, connectivity
and data availability should replicate
production. Some testing can be done
in development environment but most
of it should be done in partial or full
sandbox.
System testing is the last phase of
testing. It’s not reduce to it but
contains load testing and User
Acceptance Testing (UAT). These two
testing type require to use or create
data. As such a partial of full sandbox is
necessary

Thank You

Next event
Winter ’25 French gathering
2 octobre 2024 à partir de 18H30

Cloudity - Tour Coeur Défense
Tags