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? (30 h 01 min)

You also get:

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

Already a member? Login here

Comments & Discussion

AG
Artem Garans'kyy ✓ Link copied!

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

PK
Povilas Korop ✓ Link copied!

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.

AG
Artem Garans'kyy ✓ Link copied!

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

CO
Cristhian Ortega ✓ Link copied!

Summary:

setUp(); // It's executed once by each test case
setUpBeforeClass(); // It's executed once by the test class
J
jacm ✓ Link copied!

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?

PK
Povilas Korop ✓ Link copied!

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

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.