What is the use of test setup in Salesforce?

What is the use of test setup in Salesforce?

Methods defined with the @testSetup annotation are used for creating common test records that are available for all test methods in the class.

Is setup called for each test?

setUp() — This method is called before the invocation of each test method in the given class.

How do I use setup and tearDown?

In general you add all prerequisite steps to setUp and all clean-up steps to tearDown. You can read more with examples here. When a setUp() method is defined, the test runner will run that method prior to each test. Likewise, if a tearDown() method is defined, the test runner will invoke that method after each test.

How to create testsetup method in Test class?

You can write a method in test class, with @TestSetUp annotation applied, and create all your common test data in this method. Method marked with @TestSetUp annotation executes before any testMethod. Data created in this method doesn’t need to be created again and again, and it is by default available for all test methods.

What happens when a test setup method fails?

If a fatal error occurs during the execution of a test setup method, such as an exception that’s caused by a DML operation or an assertion failure, the entire test class fails, and no further tests in the class are executed. If a test setup method calls a non-test method of another class, no code coverage is calculated for the non-test method.

What is the syntax of a test setup method?

The following is the syntax of a test setup method. If a test class contains a test setup method, the testing framework executes the test setup method first, before any test method in the class.

Is the test setup method available for 24.0?

Test setup methods are available for 24.0 or later versions only. Every test method will get unchanged version of the test data created in setup method, doesn’t matter if any other test method has modified the data.