function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
kiran punurukiran punuru 

Where we can use seealldata = true

Hi ,
I Have one question like this :
 1: If using @istest(SeeAllData = true) in test classes is not a best practice then why it is present(why it is implemented).
 2: Which are the places where we can use @istest(SeeAllData = true)

Thanks
kiran
 
Vasani ParthVasani Parth
Kiran,

Annotating your test class or test method with IsTest(SeeAllData=true) to open up data access to records in your organization.

SeeAllData is meant as a mechanism for showing "live" data in a unit test. The better/best practice is to not specify this annotation and create your own mock data for unit tests. With your mock data, you can even set the ownership of the records and dictate what records are shared with / not shared with your code.

Second, by default unit tests run in the system context and can see any existing records (any live records if you use SeeAllData in addition to any mock records you create for testing). Sharing privileges really come into play when you use the "Test.runAs()" method and specify a particular user.

As another best practice, if you are testing any Apex classes that use the "with sharing" modifier, it's a best practice to write your unit tests, using Test.runAs for users that both have access to the records and for users that do NOT have access to those records.

Food for thought :  There is a nice blog on why not use it here (http://www.laceysnr.com/seealldata-why-i-think-you-shouldnt-use/)


Please mark this as the best answer if this helps
Vasani ParthVasani Parth
Also If you use the (SeeAllData=true) , it might happen that test class pass in sandbox but fails in production while deployment
Abhishek_DEOAbhishek_DEO
There are certian situation where we cannot create test data and have to use SeeAlldata = true.
For eg . logic written on "CollaborationGroup" depends on a particula group "name"

Here "Name" field is unique so if you create a test data with same name(to cover your logic), you will get unique constraint error. So, need to use seeAlldata=true.

There are few other scenarios discussed here (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_data_access.htm) .Pls check last para
Amit Chaudhary 8Amit Chaudhary 8
1) .User, profile, organization, AsyncApexjob, Corntrigger, RecordType, ApexClass, ApexComponent ,ApexPage we can access without (seeAllData=true) .
2). SeeAllData=true will not work for API 23 version eailer .


IsTest(SeeAllData=true) Annotation

use the isTest(SeeAllData=true) annotation to grant test classes and individual test methods access to all data in the organization,

If a test class is defined with the isTest(SeeAllData=true) annotation, this annotation applies to all its test methods whether the test methods are defined with the @isTest annotation or the testmethod keyword
The isTest(SeeAllData=true) annotation is used to open up data access when applied at the class or method level

When to use SeeAllData=true
1) To access history record
2) User last login data
3) Some time we need to create Queue ,user and some custom object date. That time mostly Mised DML error will come. To resolved that issue.

NOTE:- If you use the (SeeAllData=true) , it might happen that test class pass in sandbox but fails in production while deployment

http://www.laceysnr.com/seealldata-why-i-think-you-shouldnt-use/
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_seealldata_using.htm
http://www.ceus-now.com/test-class-seealldata-true-annotation/

Let us know if this will help you