GodziLaravel's avatar

does it better to make all related account testing in one file or on multiple files?

Hello

I'm developing a test unit for my app, now I work on the account testing :

  • sign up
  • sign in
  • sign out
  • activation email account

I'm wondering if it's a good practice to put all these test on one single file or each one on a separated file ?

Thanks

0 likes
8 replies
gych's avatar

I would create seperate test files for these since those test files will already contain multiple test methods.

For example for signup

  • log in view can be reached
  • log in works via the login view
  • log in validation works for example when user enters wrong password
gych's avatar

You can then add those files grouped together in a folder to keep the test files organized

1 like
gych's avatar

@GodziLaravel Glad I could help! Don't forget to close your thread by selecting the best answer.

If you need further assistance, don't hesitate to reach out.

martinbean's avatar

@godzilaravel Create tests for what you’re testing. If you’re testing logging in, then create a LoginTest. If you’re testing registration, create a RegisterTest.

These would also be feature tests rather than unit tests giving you’ll probably be interacting with multiple things (controllers, a database to store registered users or retrieve existing users’ credentials, etc) rather than testing a single unit of code such as a particular method.

1 like
GodziLaravel's avatar

@martinbean Thanks

Sometimes for the the current test I need to call another test , for example.

On occasion, when conducting the present test, there arises a need to invoke another test. For instance, I have a registration test and a reset password test. However, the reset password test requires an existing account. I am contemplating whether it is feasible to utilize the registration account test within the reset password test.

gych's avatar

@GodziLaravel You don't have to use the registration test for that.

For the password reset test you can create a user by using a factory for that user. To do this you have to create a factory for the User model.

By using a factory you can create the user in your password reset test method like this:

$user = User::factory()->create();

You can also create the user without a factory but that's a bad practise because you will propably need the user in multiple tests. By using a factory you can easily create a user in all those tests.

This are the docs on how to create a factory: https://laravel.com/docs/10.x/eloquent-factories

In your tests you can also add a user fthat you can use in all the methods for that test. By doing this you won't have to call the factory for each method. You add the code below at the beginning of your test class.

    private $user;

    public function setUp(): void
    {
        parent::setUp();

        $this->user = User::factory()->create();
    }

After adding this you can call the user in your test methods with $this->user

martinbean's avatar

@GodziLaravel You should not be calling one test from another. What you’re describing is merely the “arrange” part of the standard “arrange, act, assert” flow each test goes through.

If a test requires an existing account, then this is what database factories are for. You use factories to create the records you need in order to set up the “state” you want to test under.

Please or to participate in this conversation.