Skip to main content

setUp, setUpBeforeClass and Parent TestCase

Premium
4:13

The Full Lesson is Only for Premium Members

Want to access all of our courses? (36 h 00 min)

You also get:

61 courses
Premium tutorials
Access to repositories
Private Discord
Get Premium for $129/year or $29/month

Already a member? Login here

Artem Garans'kyy 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..

Povilas Korop 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.

Artem Garans'kyy 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

Cristhian Ortega avatar

Summary:

setUp(); // It's executed once by each test case
setUpBeforeClass(); // It's executed once by the test class
🥳 1
jacm 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?

Povilas Korop avatar

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