@testSetup vs DataFactory?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
6
down vote

favorite
1












When testing, how will @testSetup be preferable than a DataFactory ??
I understand how both works, but I don't see any case where I'd rather use the @testSetup annotation.



I use this datafactory and it works very well...



Thanks.










share|improve this question



























    up vote
    6
    down vote

    favorite
    1












    When testing, how will @testSetup be preferable than a DataFactory ??
    I understand how both works, but I don't see any case where I'd rather use the @testSetup annotation.



    I use this datafactory and it works very well...



    Thanks.










    share|improve this question























      up vote
      6
      down vote

      favorite
      1









      up vote
      6
      down vote

      favorite
      1






      1





      When testing, how will @testSetup be preferable than a DataFactory ??
      I understand how both works, but I don't see any case where I'd rather use the @testSetup annotation.



      I use this datafactory and it works very well...



      Thanks.










      share|improve this question













      When testing, how will @testSetup be preferable than a DataFactory ??
      I understand how both works, but I don't see any case where I'd rather use the @testSetup annotation.



      I use this datafactory and it works very well...



      Thanks.







      apex unit-test test-setup






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 19 at 13:39









      Alexis MASSON

      1178




      1178




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          12
          down vote



          accepted










          A major reason to use @TestSetup is the situation where you have many tests that require the same baseline of data. The @TestSetup method runs once and all its data is available to all the test methods in the class. Any changes made by a test method are rolled back but the @TestSetup data isn't. The benefit is faster test running because the baseline data inserts are only done once per test class not once per test method.



          A downside is that the only way to get references to the data created in @TestSetup is to query for it. But those queries are much cheaper than the inserts.



          I use @TestSetup sometimes, and other times, when I want to refer to the test data in the following asserts, use builder classes invoked from each test method so I have references to the test objects without having to do queries.



          You can choose to use whatever technique you like to create the SObjects and use that method inside the @TestStup method or outside of it.



          Some more information on @TestSetup: What are the advantage of the @testSetUp annotation.






          share|improve this answer






















          • Thanks ! Is it possible to call a @Setup from another class ? I have to test it.
            – Alexis MASSON
            Sep 19 at 14:43






          • 1




            @AlexisMASSON If you also add TestVisible you can call it from another class.
            – Keith C
            Sep 19 at 14:46










          • Another anotation I have to find explanation for, thanks for driving me in the good direction ;)
            – Alexis MASSON
            Sep 19 at 14:54

















          up vote
          8
          down vote













          I use both. Why would you have to choose one over the other? @testSetup is a way to reduce test execution time by reusing the data created once in all the test methods of a test class.



          TestFactory is to reuse same data in multiple test classes.



          Ex:



          @isTest
          private class AvinashTest
          @testSetup
          static void testSetup()
          Test.startTest();
          Account a = (Account) TestFactory.createSObject(new Account(), true);
          a.Oracle_Cust_Number__c = '10';
          update a;
          Brand__c b = (Brand__c) TestFactory.createSObject(new Brand__c(), true);
          example_Product__c rocProd = TestDataFactory.getexampleProduct( 'Test Prod', b );
          insert rocProd;
          Contact c = (Contact) TestFactory.createSObject(new Contact(), true);
          Opportunity o = (Opportunity) TestFactory.createSObject(new Opportunity(AccountId=a.Id,StageName='Closed Lost', Channel_Type__c = 'Direct'), true);
          Product2 prod1 = (Product2) TestFactory.createSObject(new Product2(Brand__c=b.Id), true);
          prod1.example_Product__c = rocProd.Id;

          update new User(Service_Provider__c = true, Id = UserInfo.getUserId());
          Test.stopTest();







          share|improve this answer




















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "459"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f233111%2ftestsetup-vs-datafactory%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            12
            down vote



            accepted










            A major reason to use @TestSetup is the situation where you have many tests that require the same baseline of data. The @TestSetup method runs once and all its data is available to all the test methods in the class. Any changes made by a test method are rolled back but the @TestSetup data isn't. The benefit is faster test running because the baseline data inserts are only done once per test class not once per test method.



            A downside is that the only way to get references to the data created in @TestSetup is to query for it. But those queries are much cheaper than the inserts.



            I use @TestSetup sometimes, and other times, when I want to refer to the test data in the following asserts, use builder classes invoked from each test method so I have references to the test objects without having to do queries.



            You can choose to use whatever technique you like to create the SObjects and use that method inside the @TestStup method or outside of it.



            Some more information on @TestSetup: What are the advantage of the @testSetUp annotation.






            share|improve this answer






















            • Thanks ! Is it possible to call a @Setup from another class ? I have to test it.
              – Alexis MASSON
              Sep 19 at 14:43






            • 1




              @AlexisMASSON If you also add TestVisible you can call it from another class.
              – Keith C
              Sep 19 at 14:46










            • Another anotation I have to find explanation for, thanks for driving me in the good direction ;)
              – Alexis MASSON
              Sep 19 at 14:54














            up vote
            12
            down vote



            accepted










            A major reason to use @TestSetup is the situation where you have many tests that require the same baseline of data. The @TestSetup method runs once and all its data is available to all the test methods in the class. Any changes made by a test method are rolled back but the @TestSetup data isn't. The benefit is faster test running because the baseline data inserts are only done once per test class not once per test method.



            A downside is that the only way to get references to the data created in @TestSetup is to query for it. But those queries are much cheaper than the inserts.



            I use @TestSetup sometimes, and other times, when I want to refer to the test data in the following asserts, use builder classes invoked from each test method so I have references to the test objects without having to do queries.



            You can choose to use whatever technique you like to create the SObjects and use that method inside the @TestStup method or outside of it.



            Some more information on @TestSetup: What are the advantage of the @testSetUp annotation.






            share|improve this answer






















            • Thanks ! Is it possible to call a @Setup from another class ? I have to test it.
              – Alexis MASSON
              Sep 19 at 14:43






            • 1




              @AlexisMASSON If you also add TestVisible you can call it from another class.
              – Keith C
              Sep 19 at 14:46










            • Another anotation I have to find explanation for, thanks for driving me in the good direction ;)
              – Alexis MASSON
              Sep 19 at 14:54












            up vote
            12
            down vote



            accepted







            up vote
            12
            down vote



            accepted






            A major reason to use @TestSetup is the situation where you have many tests that require the same baseline of data. The @TestSetup method runs once and all its data is available to all the test methods in the class. Any changes made by a test method are rolled back but the @TestSetup data isn't. The benefit is faster test running because the baseline data inserts are only done once per test class not once per test method.



            A downside is that the only way to get references to the data created in @TestSetup is to query for it. But those queries are much cheaper than the inserts.



            I use @TestSetup sometimes, and other times, when I want to refer to the test data in the following asserts, use builder classes invoked from each test method so I have references to the test objects without having to do queries.



            You can choose to use whatever technique you like to create the SObjects and use that method inside the @TestStup method or outside of it.



            Some more information on @TestSetup: What are the advantage of the @testSetUp annotation.






            share|improve this answer














            A major reason to use @TestSetup is the situation where you have many tests that require the same baseline of data. The @TestSetup method runs once and all its data is available to all the test methods in the class. Any changes made by a test method are rolled back but the @TestSetup data isn't. The benefit is faster test running because the baseline data inserts are only done once per test class not once per test method.



            A downside is that the only way to get references to the data created in @TestSetup is to query for it. But those queries are much cheaper than the inserts.



            I use @TestSetup sometimes, and other times, when I want to refer to the test data in the following asserts, use builder classes invoked from each test method so I have references to the test objects without having to do queries.



            You can choose to use whatever technique you like to create the SObjects and use that method inside the @TestStup method or outside of it.



            Some more information on @TestSetup: What are the advantage of the @testSetUp annotation.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Sep 20 at 7:13

























            answered Sep 19 at 14:07









            Keith C

            91.2k1085189




            91.2k1085189











            • Thanks ! Is it possible to call a @Setup from another class ? I have to test it.
              – Alexis MASSON
              Sep 19 at 14:43






            • 1




              @AlexisMASSON If you also add TestVisible you can call it from another class.
              – Keith C
              Sep 19 at 14:46










            • Another anotation I have to find explanation for, thanks for driving me in the good direction ;)
              – Alexis MASSON
              Sep 19 at 14:54
















            • Thanks ! Is it possible to call a @Setup from another class ? I have to test it.
              – Alexis MASSON
              Sep 19 at 14:43






            • 1




              @AlexisMASSON If you also add TestVisible you can call it from another class.
              – Keith C
              Sep 19 at 14:46










            • Another anotation I have to find explanation for, thanks for driving me in the good direction ;)
              – Alexis MASSON
              Sep 19 at 14:54















            Thanks ! Is it possible to call a @Setup from another class ? I have to test it.
            – Alexis MASSON
            Sep 19 at 14:43




            Thanks ! Is it possible to call a @Setup from another class ? I have to test it.
            – Alexis MASSON
            Sep 19 at 14:43




            1




            1




            @AlexisMASSON If you also add TestVisible you can call it from another class.
            – Keith C
            Sep 19 at 14:46




            @AlexisMASSON If you also add TestVisible you can call it from another class.
            – Keith C
            Sep 19 at 14:46












            Another anotation I have to find explanation for, thanks for driving me in the good direction ;)
            – Alexis MASSON
            Sep 19 at 14:54




            Another anotation I have to find explanation for, thanks for driving me in the good direction ;)
            – Alexis MASSON
            Sep 19 at 14:54












            up vote
            8
            down vote













            I use both. Why would you have to choose one over the other? @testSetup is a way to reduce test execution time by reusing the data created once in all the test methods of a test class.



            TestFactory is to reuse same data in multiple test classes.



            Ex:



            @isTest
            private class AvinashTest
            @testSetup
            static void testSetup()
            Test.startTest();
            Account a = (Account) TestFactory.createSObject(new Account(), true);
            a.Oracle_Cust_Number__c = '10';
            update a;
            Brand__c b = (Brand__c) TestFactory.createSObject(new Brand__c(), true);
            example_Product__c rocProd = TestDataFactory.getexampleProduct( 'Test Prod', b );
            insert rocProd;
            Contact c = (Contact) TestFactory.createSObject(new Contact(), true);
            Opportunity o = (Opportunity) TestFactory.createSObject(new Opportunity(AccountId=a.Id,StageName='Closed Lost', Channel_Type__c = 'Direct'), true);
            Product2 prod1 = (Product2) TestFactory.createSObject(new Product2(Brand__c=b.Id), true);
            prod1.example_Product__c = rocProd.Id;

            update new User(Service_Provider__c = true, Id = UserInfo.getUserId());
            Test.stopTest();







            share|improve this answer
























              up vote
              8
              down vote













              I use both. Why would you have to choose one over the other? @testSetup is a way to reduce test execution time by reusing the data created once in all the test methods of a test class.



              TestFactory is to reuse same data in multiple test classes.



              Ex:



              @isTest
              private class AvinashTest
              @testSetup
              static void testSetup()
              Test.startTest();
              Account a = (Account) TestFactory.createSObject(new Account(), true);
              a.Oracle_Cust_Number__c = '10';
              update a;
              Brand__c b = (Brand__c) TestFactory.createSObject(new Brand__c(), true);
              example_Product__c rocProd = TestDataFactory.getexampleProduct( 'Test Prod', b );
              insert rocProd;
              Contact c = (Contact) TestFactory.createSObject(new Contact(), true);
              Opportunity o = (Opportunity) TestFactory.createSObject(new Opportunity(AccountId=a.Id,StageName='Closed Lost', Channel_Type__c = 'Direct'), true);
              Product2 prod1 = (Product2) TestFactory.createSObject(new Product2(Brand__c=b.Id), true);
              prod1.example_Product__c = rocProd.Id;

              update new User(Service_Provider__c = true, Id = UserInfo.getUserId());
              Test.stopTest();







              share|improve this answer






















                up vote
                8
                down vote










                up vote
                8
                down vote









                I use both. Why would you have to choose one over the other? @testSetup is a way to reduce test execution time by reusing the data created once in all the test methods of a test class.



                TestFactory is to reuse same data in multiple test classes.



                Ex:



                @isTest
                private class AvinashTest
                @testSetup
                static void testSetup()
                Test.startTest();
                Account a = (Account) TestFactory.createSObject(new Account(), true);
                a.Oracle_Cust_Number__c = '10';
                update a;
                Brand__c b = (Brand__c) TestFactory.createSObject(new Brand__c(), true);
                example_Product__c rocProd = TestDataFactory.getexampleProduct( 'Test Prod', b );
                insert rocProd;
                Contact c = (Contact) TestFactory.createSObject(new Contact(), true);
                Opportunity o = (Opportunity) TestFactory.createSObject(new Opportunity(AccountId=a.Id,StageName='Closed Lost', Channel_Type__c = 'Direct'), true);
                Product2 prod1 = (Product2) TestFactory.createSObject(new Product2(Brand__c=b.Id), true);
                prod1.example_Product__c = rocProd.Id;

                update new User(Service_Provider__c = true, Id = UserInfo.getUserId());
                Test.stopTest();







                share|improve this answer












                I use both. Why would you have to choose one over the other? @testSetup is a way to reduce test execution time by reusing the data created once in all the test methods of a test class.



                TestFactory is to reuse same data in multiple test classes.



                Ex:



                @isTest
                private class AvinashTest
                @testSetup
                static void testSetup()
                Test.startTest();
                Account a = (Account) TestFactory.createSObject(new Account(), true);
                a.Oracle_Cust_Number__c = '10';
                update a;
                Brand__c b = (Brand__c) TestFactory.createSObject(new Brand__c(), true);
                example_Product__c rocProd = TestDataFactory.getexampleProduct( 'Test Prod', b );
                insert rocProd;
                Contact c = (Contact) TestFactory.createSObject(new Contact(), true);
                Opportunity o = (Opportunity) TestFactory.createSObject(new Opportunity(AccountId=a.Id,StageName='Closed Lost', Channel_Type__c = 'Direct'), true);
                Product2 prod1 = (Product2) TestFactory.createSObject(new Product2(Brand__c=b.Id), true);
                prod1.example_Product__c = rocProd.Id;

                update new User(Service_Provider__c = true, Id = UserInfo.getUserId());
                Test.stopTest();








                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Sep 19 at 14:09









                Avinash

                1,1231619




                1,1231619



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f233111%2ftestsetup-vs-datafactory%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    How to check contact read email or not when send email to Individual?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?