ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Test(테스트, TDD)
    Laravel 2021. 2. 5. 15:38
    반응형
    SMALL

    # What?

    - 테스트 케이스를 만들고 해당 테스트 케이스를 충족하는 기능을 개발한 후 테스트 케이스를 돌려 제대로 동작하는지 확인

     

    # How?

    1. 테스트케이스 생성

    php artisan make:test PortfoliosTest

    2. DB용 데이터베이스 세팅(DB_CONNECTION, DB_DATABASE)

    <?xml version="1.0" encoding="UTF-8"?>
    <phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
             bootstrap="vendor/autoload.php"
             colors="true"
    >
        <testsuites>
            <testsuite name="Unit">
                <directory suffix="Test.php">./tests/Unit</directory>
            </testsuite>
            <testsuite name="Feature">
                <directory suffix="Test.php">./tests/Feature</directory>
            </testsuite>
        </testsuites>
        <coverage processUncoveredFiles="true">
            <include>
                <directory suffix=".php">./app</directory>
            </include>
        </coverage>
        <php>
            <server name="APP_ENV" value="testing"/>
            <server name="BCRYPT_ROUNDS" value="4"/>
            <server name="CACHE_DRIVER" value="array"/>
            <server name="DB_CONNECTION" value="sqlite"/>
            <server name="DB_DATABASE" value=":memory:"/>
            <server name="MAIL_MAILER" value="array"/>
            <server name="QUEUE_CONNECTION" value="sync"/>
            <server name="SESSION_DRIVER" value="array"/>
            <server name="TELESCOPE_ENABLED" value="false"/>
        </php>
    </phpunit>
    

     

    3. 테스트케이스 작성

    => RefreshDatabase 추가해서 실행할때마다 데이터베이스 초기화시키기(실서버에 돌리면 절대 안됨)

    => 자주 사용할 세팅은 setUp에 설정해놓기

     

    * 테스트 케이스 위에 /** @test */라고 붙여줘야함

    <?php
    
    namespace Tests\Feature;
    
    use App\Models\User;
    use Database\Factories\UserFactory;
    use Illuminate\Foundation\Testing\RefreshDatabase;
    use Illuminate\Foundation\Testing\WithFaker;
    use Tests\TestCase;
    
    class PortfoliosTest extends TestCase
    {
        use RefreshDatabase;
    
        protected $user;
    
        protected function setUp(): void
        {
            parent::setUp();
            
            $this->user = User::factory()->create();
    
            // 로그인처리
            $this->actingAs($this->user);
        }
    
        public function test_example()
        {
            $response = $this->get('/');
    
            $response->assertStatus(200);
        }
    
        /** @test */
        public function 테스트다()
        {
            $response = $this->get('/');
    
            $response->assertStatus(200);
        }
    }
    

     

    4. 테스트 돌리기

    php artisan test

    * 매번 php artisan test 써주기 귀찮으면 alias p="php artisan test" 이런식으로 사용

    * 가끔 연관관계 관련해서 작업할 때 싱크 안맞을 때 있음. 지웠는데도 안지워진걸로 나타난다던지. 그럴때는 load 사용

    $this->user->load("products")->products
    LIST

    'Laravel' 카테고리의 다른 글

    한 컨트롤러에서 여러 모델에 대한 pagination 사용하고 싶을 때(페이지네이션)  (0) 2021.11.16
    Test 405 Method 오류날 경우  (0) 2021.06.02
    인스타그램 API 연동  (2) 2021.02.05
    Mail(메일)  (0) 2020.11.01
    whereExists  (0) 2020.08.18

    댓글

Designed by Tistory.