Courses

Advanced Laravel Testing

setUp, setUpBeforeClass and Parent TestCase

Previous: Response Debugging: Dump and Die
avatar

Hi Povilas. I try to use this. But it doesn't work. I am put SetUp function to parrent class but SetUp run enyway for each test..

avatar

Maybe you name it differently? It should be setUp() not SetUp() Just guessing, it's hard to debug for you remotely what you did wrong.

avatar

Thanks for reply.

abstract class TestCase extends BaseTestCase
 {
     use CreatesApplication; 
     /**
      * Indicates whether the default seeder should run before each test.
      *
      * @var bool
@@      */
     protected $seed = true;
     protected function setUp(): void
     {
         parent::setUp();
         echo 'run SetUp';
         // TODO: Change the autogenerated stub
			}
 }

This is my base class. I am put correct setUp(). And I see "run SetUp" each test.

I found another way how use same user in my tests. stackoverflow.com

avatar
You can use Markdown
avatar

Summary:

setUp();  // It's executed once by each test case
setUpBeforeClass();  // It's executed once by the test class
🥳 1
avatar
You can use Markdown
avatar

Hello, excellent tip, but how do I do it if I want to use the same function in several Test Classes, but not in all of them?

avatar

I guess then you need to use/call that function specifically in the classes where you need it.

avatar
You can use Markdown
avatar
You can use Markdown