개발! 딱 깔끔하고 센스있게!

라 와 빌 에 PHP 유닛 테스트 구축 본문

개발 스크랩 메모/PHP

라 와 빌 에 PHP 유닛 테스트 구축

렉사이 2020. 11. 26. 23:51

유닛 테스트 에 익숙 하지 않 지만, punt. de 에 관 한 거의 모든 문서 (10 장 까지) 를 읽 었 습 니 다.

데이터 베 이 스 를 이용 해 테스트 하 는 것 은 느 릴 수 있 지만, 정확 한 설정 을 하면 비 데이터 베 이 스 를 테스트 하 는 것 과 마찬가지 로 빠르다 는 것 이다.

그래서 라 르 빌 에서 모델 을 시험 해 보고 싶 어 요.나 는 데 이 터 를 데이터베이스 에 심 어 주기 위해 모델 공장 을 만 들 었 다.

나 는 기본 테스트 도 만 들 었 다.

PHPUnts 문서 에 서 는 테스트 할 때마다 setup () 방법 으로 테스트 를 설정 한 다 는 내용 을 담 고 있 습 니 다.

또 다른 정적 방법 이 있 습 니 다.

set up. Beforleass ().

데이터베이스 시트 에 피 드 를 한 번 만 설치 하고 테스트 에 기록 하고 싶 습 니 다.

그래서 저 는 Laravels factory () 함 수 를 사용 합 니 다 setup Befor Class () 방법 에서 데이터베이스 피 드 를 사용 합 니 다.

이것 은 나의 코드 입 니 다:

class CommentTest extends TestCase { protected static $blog; protected static $comments; public static function setUpBeforeClass() { parent::setUpBeforeClass(); self::$blog = factory(App\Models\Content\Blog::class)->create(); self::$comments = factory(App\Models\Content\Comment::class, 6)->create(); } public function testSomething() { $this->assertTrue(true); } } 

하지만 punt 을 실행 할 때 다음 과 같은 오 류 를 받 습 니 다:

Fatal error: Call to a member function make() on a non-object in \vendor\laravel\framework\src\Illuminate\Foundation\helpers.php on line 54 Call Stack: 0.0002 240752 1. {main}() \vendor\phpunit\phpunit\phpunit:0 0.0173 1168632 2. PHPUnit_TextUI_Command::main() \vendor\phpunit\phpunit\phpunit:47 0.0173 1175304 3. PHPUnit_TextUI_Command->run() \vendor\phpunit\phpunit\src\TextUI\Command.php:100 2.9397 5869416 4. PHPUnit_TextUI_TestRunner->doRun() \vendor\phpunit\phpunit\src\TextUI\Command.php:149 2.9447 6077272 5. PHPUnit_Framework_TestSuite->run() \vendor\phpunit\phpunit\src\TextUI\TestRunner.php:440 2.9459 6092880 6. PHPUnit_Framework_TestSuite->run() \vendor\phpunit\phpunit\src\Framework\TestSuite.php:747 2.9555 6096160 7. call_user_func:{\vendor\phpunit\phpunit\src\Framework\TestSuite.php:697}() \vendor\phpunit\phpunit\src\Framework\TestSuite.php:697 2.9555 6096272 8. CommentTest::setUpBeforeClass() \vendor\phpunit\phpunit\src\Framework\TestSuite.php:697 2.9555 6096480 9. factory() \tests\CommentTest.php:18 2.9556 6096656 10. app() \vendor\laravel\framework\src\Illuminate\Foundation\helpers.php:350 

setup: Beforgrass () 에서 setup () 로 코드 를 옮 겨 서 실행 하면 예 상 했 던 대로 실행 할 수 있 습 니 다.

하지만 이것 은 분명 저 효 과 를 볼 수 있 습 니 다.

모든 테스트 에 데이터 베 이 스 를 뿌 렸 기 때 문 입 니까?

나의 문제:

  1. Is seeding the database from within the setUpBeforeClass() the correct way to do this?
  2. If it is (question 1), then why am I getting the fatal error when running phpunit, and is there anything I should be doing before calling factory()?
  3. If I do have to place the code in the setUp() method, are there going to be performance issues?
  4. Should I even be seeding from the setUpBeforeClass() or setUp() methods? In Laravels documentation it shows examples where the seeding is happening in the test itself, but If i'm running 100 tests (for example), is it a good idea to be seeding 100 times?

대답 하 다.

알 겠 습 니 다.

약간의 조 사 를 통 해 저 는 정적 인 setup BeforleClass () 방법 을 호출 할 때 Laravel 응용 프로그램 을 만 들 지 않 았 습 니 다.

처음으로 \ vendor \ Laravel \ from Work \ src \ illumed \ Foundation \ Testing 에서 set Up () 을 호출 할 때 Laravel 용 기 를 만 듭 니 다 \테스트 케이스...코드 를 setup () 방법 으로 옮 겼 을 때 제대로 작 동 할 수 있 는 이유 다.

용 기 는 $app 속성 에 저 장 됩 니 다.

\ vendor \ lavel \ from Work \ src \ illumed \ Foundation \ Testing 에 저 장 됩 니 다.

\Application Traint. phop 응용 프로그램...

이 코드 를 setup Beforgrass () 에 추가 하 는 방법 을 통 해 수 동 으로 용기 인 스 턴 스 를 생 성 할 수 있 습 니 다:

$app = require __DIR__.'/../bootstrap/app.php'; $app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap(); 

그러나 이 방법 은 매우 진부 해 보 여서 나 는 좋아 하지 않 는 다.

반대로 피 드 설정 코드 를 setUp () 방법 으로 옮 겼 지만, 속성 이 비어 있 을 때 만 피 드 를 데이터베이스 로 설정 합 니 다.

이 때문에 setup () 을 처음 호출 할 때 만 씨 를 뿌리 게 된다.

그 어떠한 후속 호출 에 도 피 드 는 삽입 되 지 않 습 니 다:

class CommentTest extends TestCase { use DatabaseMigrations; protected static $blog; protected static $comments; public function setUp() { parent::setUp(); $this->runDatabaseMigrations(); if (is_null(self::$blog)) { self::$blog = factory(App\Models\Content\Blog::class, 1)->create(); self::$comments = factory(App\Models\Content\Comment::class, 6)->create(); } } } 

테스트 에 사용 되 는 Laravels 데이터베이스 이전 특성 과 결합 하여 현재 작업 흐름:

  1. PHPUnit is called
  2. The Test class is called, which contains the DatabaseMigrations trait
  3. The database is migrated (tables created)
  4. The setUp() method is called for the first time, which seeds the relevant tables with testing data
  5. The test is run, and and access the test data
  6. There is no tearDown() method invoked, instead the DatabaseMigrations trait simply resets the database, so my test doesn't have to worry about cleaning up the test data.

편집

또한, (내 가 100% 는 아니 지만) 사용자 정의 setup () 방법 이 있다 면, 다시 쓰 는 setup () 방법 에서 rundatabasemgrations () 를 수 동 으로 호출 하 십시오:

public function setUp() { parent::setUp(); $this->runDatabaseMigrations(); /** Rest of Setup **/ } 

set Up () 방법 을 다시 불 러 오 면 rundatabase Migrations () 는 자동 으로 호출 되 지 않 을 것 같 습 니 다.

나 는 이것 이 도움 이 되 기 를 바 랍 니 다.

그러나 다른 사람 이 더 좋 은 해결 방안 이 있 으 면 언제든지 저 에 게 알려 주 십시오.)

Comments