2

I'm looking at the study guide for the Platform Developer 1 certification. Under the Testing section one of the headings is:

Describe the difference between invoking Apex in execute anonymous vs. unit tests.

I've found very little on the subject apart from the following forum post.

My question is, apart from the permissions it is run under and the fact unit test data is transient, what other differences are important to note between the two?

Is there anything else that can be used to compare the two methods of invocation?

1
  • 1
    Data? execute anonymous is like running test as @isTest(SeeAllData=true)?
    – Girbot
    Aug 19 '16 at 16:42
3

Unit Test (UT) vs. Execute Anonymous (EA)

Here's pretty much all the differences I could think of off the top of my head. They'll probably ask about some specific sub-detail; it really just helps to know what UT and EA are used for.

Callouts

UT: Mock callout, no actual systems are affected.

EA: Live callout, external data may be affected.

Data Isolation

UT: No records modified will affect live data, despite successful execution.

EA: All records modified will affect live data upon successful execution.

Governor Limits

UT: Can be reset mid-way to mock a new transaction.

EA: Subject to normal synchronous limits.

Emulate User

UT: Can use System.runAs to pretend to be another (possibly non-existent) user.

EA: Calls are always made under your own login information.

Field Level Security

UT: Can use all fields and objects as if you were in System Mode.

EA: Cannot use fields and objects you cannot see, which can cause compilation errors.

Default Sharing Mode

UT: Uses System Mode ("without sharing") by default.

EA: Uses User Mode ("with sharing") by default.

Code Coverage

UT: Provides code coverage for non-UT code.

EA: Does not provide code coverage for non-UT code (as it cannot be run during a deployment).

0

Well, the first thing that comes to mind is that anonymous scripts cannot protect critical application functionality during deployments. Unit Tests which use assertions liberally form a contract that defines how the system must behave. This contract is enforced automatically during production deployments, and can be enforced during sandbox deployments by opting to run all tests.

See also How to Write Good Unit Tests

The Value of Unit Tests

One of the most valuable benefits of unit tests is that they give you confidence that your code works as you expect it to work. Unit tests give you the confidence to do long-term development because with unit tests in place, you know that your foundation code is dependable. Unit tests give you the confidence to refactor your code to make it cleaner and more efficient.

Unit tests also save you time because unit tests help prevent regressions from being introduced and released. Once a bug is found, you can write a unit test for it, you can fix the bug, and the bug can never make it to production again because the unit tests will catch it in the future.

Another advantage is that unit tests provide excellent implicit documentation because they show exactly how the code is designed to be used.

Many developers have discovered these advantages. Some developers, including me, believe so strongly in the value of unit tests that we write our tests first and our production code second. I’m going to show you an example of this test-first development practice a little later on in the article.

1
  • 1
    Addition to this, Unit Testing is Testing your application using the apex code. Every scenario/logic is verified using apex code. It act as a Regression Testing. A single validation rule can break the system. Using Unit Test, we can identify such break down well in advance. Unit Test involves Positive-Negative Scenarios, Performance on Single-Bulk records, Run the system in context of a specific user.
    – Devendra
    Aug 19 '16 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy