Hướng dẫn phpunit teardown after all tests - phpunit teardown sau tất cả các thử nghiệm

Tôi đã đến trang này với cùng một câu hỏi, tuy nhiên câu trả lời được chấp nhận được chạy trên tất cả các lớp và đối với tôi không phải là câu trả lời chính xác.

Nếu bạn giống tôi, "bài kiểm tra tích hợp" đầu tiên của bạn là xóa DB và chạy di chuyển. Điều này có được chính bạn tại một cơ sở dữ liệu cho tất cả các bài kiểm tra. Tôi liên tục thay đổi các tệp di chuyển vào thời điểm này, vì vậy việc thiết lập đường cơ sở thực sự là một phần của tất cả các bài kiểm tra.

Việc di chuyển mất một lúc, vì vậy tôi không muốn nó chạy trên tất cả các bài kiểm tra.

Sau đó, tôi cần xây dựng cơ sở dữ liệu thử nghiệm từng phần. Tôi cần viết một bài kiểm tra đơn hàng, nhưng trước tiên tôi cần tạo một số sản phẩm và kiểm tra điều đó, sau đó tôi cần kiểm tra một kết quả nhập khẩu.

Vì vậy, những gì tôi đã làm là siêu dễ dàng, nhưng không được giải thích cực kỳ tốt trên internet. Tôi đã tạo một thử nghiệm đơn giản để thiết lập cơ sở dữ liệu. Sau đó, trong tệp phpspec.xml của bạn, thêm một testsuite ....


    tests/in/SystemSetupTest.php
    tests/in/ProductTest.php
    tests/in/ProductImportTest.php

Và trong systemsetuptest.php ....

class SystemSetupTest extends ApiTester
{

    /** @test */
    function system_init()
    {
        fwrite(STDOUT, __METHOD__ . "\n");
        self::createEM(); //this has all the code to init the system...
    }
}

Sau đó thực thi nó như:

PHPUNIT -Các sản phẩm thử nghiệm

Cuối cùng, nó dễ dàng hơn. Nó sẽ cho phép tôi xây dựng hệ thống của mình một cách chính xác.

Ngoài ra, tôi đang sử dụng Laravel 5. Khi sử dụng setUpBeforeClass(), tôi kết thúc với các vấn đề về Bootstrap, mà tôi chắc chắn rằng tôi có thể khắc phục, nhưng phương pháp tôi sử dụng ở trên hoạt động hoàn hảo.

Một trong những phần tốn nhiều thời gian nhất của các bài kiểm tra viết là viết mã để thiết lập thế giới ở trạng thái đã biết và sau đó đưa nó trở lại trạng thái ban đầu khi bài kiểm tra hoàn tất. Trạng thái được biết đến này được gọi là vật cố của bài kiểm tra.

Trong thử nghiệm các hoạt động mảng với phpunit, vật cố là mảng được lưu trữ trong biến $stack. Tuy nhiên, hầu hết thời gian, vật cố sẽ phức tạp hơn một mảng đơn giản và lượng mã cần thiết để thiết lập nó sẽ phát triển tương ứng. Nội dung thực tế của bài kiểm tra bị mất trong tiếng ồn khi thiết lập vật cố. Vấn đề này thậm chí còn tồi tệ hơn khi bạn viết một số bài kiểm tra với đồ đạc tương tự. Nếu không có một số trợ giúp từ khung thử nghiệm, chúng tôi sẽ phải sao chép mã thiết lập vật cố cho mỗi bài kiểm tra chúng tôi viết.Testing array operations with PHPUnit, the fixture was the array that is stored in the $stack variable. Most of the time, though, the fixture will be more complex than a simple array, and the amount of code needed to set it up will grow accordingly. The actual content of the test gets lost in the noise of setting up the fixture. This problem gets even worse when you write several tests with similar fixtures. Without some help from the testing framework, we would have to duplicate the code that sets up the fixture for each test we write.

PHPUNIT hỗ trợ chia sẻ mã thiết lập. Trước khi một phương thức kiểm tra được chạy, một phương thức mẫu gọi là

class SystemSetupTest extends ApiTester
{

    /** @test */
    function system_init()
    {
        fwrite(STDOUT, __METHOD__ . "\n");
        self::createEM(); //this has all the code to init the system...
    }
}
0 được gọi.
class SystemSetupTest extends ApiTester
{

    /** @test */
    function system_init()
    {
        fwrite(STDOUT, __METHOD__ . "\n");
        self::createEM(); //this has all the code to init the system...
    }
}
0 là nơi bạn tạo các đối tượng mà bạn sẽ kiểm tra. Khi phương thức kiểm tra đã hoàn thành chạy, cho dù nó thành công hay thất bại, một phương thức mẫu khác được gọi là
class SystemSetupTest extends ApiTester
{

    /** @test */
    function system_init()
    {
        fwrite(STDOUT, __METHOD__ . "\n");
        self::createEM(); //this has all the code to init the system...
    }
}
2 được gọi.
class SystemSetupTest extends ApiTester
{

    /** @test */
    function system_init()
    {
        fwrite(STDOUT, __METHOD__ . "\n");
        self::createEM(); //this has all the code to init the system...
    }
}
2 là nơi bạn làm sạch các đối tượng mà bạn đã thử nghiệm.

Khi sử dụng chú thích @depends để thể hiện sự phụ thuộc, chúng tôi đã sử dụng mối quan hệ người tiêu dùng nhà sản xuất giữa các thử nghiệm để chia sẻ một trận đấu. Điều này không phải lúc nào cũng mong muốn hoặc thậm chí có thể. Ví dụ 4.1 cho thấy cách chúng ta có thể viết các bài kiểm tra của

class SystemSetupTest extends ApiTester
{

    /** @test */
    function system_init()
    {
        fwrite(STDOUT, __METHOD__ . "\n");
        self::createEM(); //this has all the code to init the system...
    }
}
4 theo cách mà không phải bản thân được sử dụng lại mà là mã tạo ra nó. Đầu tiên chúng tôi khai báo biến thể hiện, $stack, mà chúng tôi sẽ sử dụng thay vì biến phương pháp địa phương. Sau đó, chúng tôi đặt việc tạo ra vật cố
class SystemSetupTest extends ApiTester
{

    /** @test */
    function system_init()
    {
        fwrite(STDOUT, __METHOD__ . "\n");
        self::createEM(); //this has all the code to init the system...
    }
}
6 vào phương pháp
class SystemSetupTest extends ApiTester
{

    /** @test */
    function system_init()
    {
        fwrite(STDOUT, __METHOD__ . "\n");
        self::createEM(); //this has all the code to init the system...
    }
}
0. Cuối cùng, chúng tôi loại bỏ mã dự phòng khỏi các phương thức thử nghiệm và sử dụng biến thể hiện mới được giới thiệu,
class SystemSetupTest extends ApiTester
{

    /** @test */
    function system_init()
    {
        fwrite(STDOUT, __METHOD__ . "\n");
        self::createEM(); //this has all the code to init the system...
    }
}
8, thay vì biến phương pháp địa phương $stack với phương thức khẳng định
php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

final class StackTest extends TestCase
{
    private $stack;

    protected function setUp(): void
    {
        $this->stack = [];
    }

    public function testEmpty(): void
    {
        $this->assertTrue(empty($this->stack));
    }

    public function testPush(): void
    {
        array_push($this->stack, 'foo');

        $this->assertSame('foo', $this->stack[count($this->stack)-1]);
        $this->assertFalse(empty($this->stack));
    }

    public function testPop(): void
    {
        array_push($this->stack, 'foo');

        $this->assertSame('foo', array_pop($this->stack));
        $this->assertTrue(empty($this->stack));
    }
}
0.Using the @depends annotation to express dependencies we used the producer-consumer relationship between tests to share a fixture. This is not always desired or even possible. Example 4.1 shows how we can write the tests of the
class SystemSetupTest extends ApiTester
{

    /** @test */
    function system_init()
    {
        fwrite(STDOUT, __METHOD__ . "\n");
        self::createEM(); //this has all the code to init the system...
    }
}
4 in such a way that not the fixture itself is reused but the code that creates it. First we declare the instance variable, $stack, that we are going to use instead of a method-local variable. Then we put the creation of the
class SystemSetupTest extends ApiTester
{

    /** @test */
    function system_init()
    {
        fwrite(STDOUT, __METHOD__ . "\n");
        self::createEM(); //this has all the code to init the system...
    }
}
6 fixture into the
class SystemSetupTest extends ApiTester
{

    /** @test */
    function system_init()
    {
        fwrite(STDOUT, __METHOD__ . "\n");
        self::createEM(); //this has all the code to init the system...
    }
}
0 method. Finally, we remove the redundant code from the test methods and use the newly introduced instance variable,
class SystemSetupTest extends ApiTester
{

    /** @test */
    function system_init()
    {
        fwrite(STDOUT, __METHOD__ . "\n");
        self::createEM(); //this has all the code to init the system...
    }
}
8, instead of the method-local variable $stack with the
php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

final class StackTest extends TestCase
{
    private $stack;

    protected function setUp(): void
    {
        $this->stack = [];
    }

    public function testEmpty(): void
    {
        $this->assertTrue(empty($this->stack));
    }

    public function testPush(): void
    {
        array_push($this->stack, 'foo');

        $this->assertSame('foo', $this->stack[count($this->stack)-1]);
        $this->assertFalse(empty($this->stack));
    }

    public function testPop(): void
    {
        array_push($this->stack, 'foo');

        $this->assertSame('foo', array_pop($this->stack));
        $this->assertTrue(empty($this->stack));
    }
}
0 assertion method.

Ví dụ 4.1 Sử dụng Setup () để tạo Stack Fresture¶Using setUp() to create the stack fixture

php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

final class StackTest extends TestCase
{
    private $stack;

    protected function setUp(): void
    {
        $this->stack = [];
    }

    public function testEmpty(): void
    {
        $this->assertTrue(empty($this->stack));
    }

    public function testPush(): void
    {
        array_push($this->stack, 'foo');

        $this->assertSame('foo', $this->stack[count($this->stack)-1]);
        $this->assertFalse(empty($this->stack));
    }

    public function testPop(): void
    {
        array_push($this->stack, 'foo');

        $this->assertSame('foo', array_pop($this->stack));
        $this->assertTrue(empty($this->stack));
    }
}

Các phương thức mẫu

class SystemSetupTest extends ApiTester
{

    /** @test */
    function system_init()
    {
        fwrite(STDOUT, __METHOD__ . "\n");
        self::createEM(); //this has all the code to init the system...
    }
}
0 và
class SystemSetupTest extends ApiTester
{

    /** @test */
    function system_init()
    {
        fwrite(STDOUT, __METHOD__ . "\n");
        self::createEM(); //this has all the code to init the system...
    }
}
2 được chạy một lần cho mỗi phương pháp thử nghiệm (và trên các trường hợp mới) của lớp trường hợp thử nghiệm.

Ngoài ra, các phương thức mẫu

php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

final class StackTest extends TestCase
{
    private $stack;

    protected function setUp(): void
    {
        $this->stack = [];
    }

    public function testEmpty(): void
    {
        $this->assertTrue(empty($this->stack));
    }

    public function testPush(): void
    {
        array_push($this->stack, 'foo');

        $this->assertSame('foo', $this->stack[count($this->stack)-1]);
        $this->assertFalse(empty($this->stack));
    }

    public function testPop(): void
    {
        array_push($this->stack, 'foo');

        $this->assertSame('foo', array_pop($this->stack));
        $this->assertTrue(empty($this->stack));
    }
}
3 và
php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

final class StackTest extends TestCase
{
    private $stack;

    protected function setUp(): void
    {
        $this->stack = [];
    }

    public function testEmpty(): void
    {
        $this->assertTrue(empty($this->stack));
    }

    public function testPush(): void
    {
        array_push($this->stack, 'foo');

        $this->assertSame('foo', $this->stack[count($this->stack)-1]);
        $this->assertFalse(empty($this->stack));
    }

    public function testPop(): void
    {
        array_push($this->stack, 'foo');

        $this->assertSame('foo', array_pop($this->stack));
        $this->assertTrue(empty($this->stack));
    }
}
4 được gọi trước khi thử nghiệm đầu tiên của lớp trường hợp thử nghiệm được chạy và sau khi thử nghiệm cuối cùng của lớp trường hợp thử nghiệm được chạy, tương ứng.

Ví dụ dưới đây cho thấy tất cả các phương thức mẫu có sẵn trong một lớp trường hợp thử nghiệm.

Ví dụ 4.2 Ví dụ Hiển thị tất cả các phương thức mẫu có sẵn vàExample showing all template methods available

php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

final class TemplateMethodsTest extends TestCase
{
    public static function setUpBeforeClass(): void
    {
        fwrite(STDOUT, __METHOD__ . "\n");
    }

    protected function setUp(): void
    {
        fwrite(STDOUT, __METHOD__ . "\n");
    }

    protected function assertPreConditions(): void
    {
        fwrite(STDOUT, __METHOD__ . "\n");
    }

    public function testOne(): void
    {
        fwrite(STDOUT, __METHOD__ . "\n");
        $this->assertTrue(true);
    }

    public function testTwo(): void
    {
        fwrite(STDOUT, __METHOD__ . "\n");
        $this->assertTrue(false);
    }

    protected function assertPostConditions(): void
    {
        fwrite(STDOUT, __METHOD__ . "\n");
    }

    protected function tearDown(): void
    {
        fwrite(STDOUT, __METHOD__ . "\n");
    }

    public static function tearDownAfterClass(): void
    {
        fwrite(STDOUT, __METHOD__ . "\n");
    }

    protected function onNotSuccessfulTest(Throwable $t): void
    {
        fwrite(STDOUT, __METHOD__ . "\n");
        throw $t;
    }
}

$ phpunit TemplateMethodsTest
PHPUnit 9.5.0 by Sebastian Bergmann and contributors.

TemplateMethodsTest::setUpBeforeClass
TemplateMethodsTest::setUp
TemplateMethodsTest::assertPreConditions
TemplateMethodsTest::testOne
TemplateMethodsTest::assertPostConditions
TemplateMethodsTest::tearDown
.TemplateMethodsTest::setUp
TemplateMethodsTest::assertPreConditions
TemplateMethodsTest::testTwo
TemplateMethodsTest::tearDown
TemplateMethodsTest::onNotSuccessfulTest
FTemplateMethodsTest::tearDownAfterClass

Time: 0 seconds, Memory: 5.25Mb

There was 1 failure:

1) TemplateMethodsTest::testTwo
Failed asserting that  is true.
/home/sb/TemplateMethodsTest.php:30

FAILURES!
Tests: 2, Assertions: 2, Failures: 1.

Thiết lập nhiều hơn () hơn rehortdown () ¶

class SystemSetupTest extends ApiTester
{

    /** @test */
    function system_init()
    {
        fwrite(STDOUT, __METHOD__ . "\n");
        self::createEM(); //this has all the code to init the system...
    }
}
0 và
class SystemSetupTest extends ApiTester
{

    /** @test */
    function system_init()
    {
        fwrite(STDOUT, __METHOD__ . "\n");
        self::createEM(); //this has all the code to init the system...
    }
}
2 đối xứng độc đáo về lý thuyết, nhưng không có trong thực tế. Trong thực tế, bạn chỉ cần triển khai
class SystemSetupTest extends ApiTester
{

    /** @test */
    function system_init()
    {
        fwrite(STDOUT, __METHOD__ . "\n");
        self::createEM(); //this has all the code to init the system...
    }
}
2 nếu bạn đã phân bổ các tài nguyên bên ngoài như tệp hoặc ổ cắm trong ____10. Nếu
class SystemSetupTest extends ApiTester
{

    /** @test */
    function system_init()
    {
        fwrite(STDOUT, __METHOD__ . "\n");
        self::createEM(); //this has all the code to init the system...
    }
}
0 của bạn chỉ tạo các đối tượng PHP đơn giản, bạn thường có thể bỏ qua
class SystemSetupTest extends ApiTester
{

    /** @test */
    function system_init()
    {
        fwrite(STDOUT, __METHOD__ . "\n");
        self::createEM(); //this has all the code to init the system...
    }
}
2.

Tuy nhiên, nếu bạn tạo nhiều đối tượng trong

class SystemSetupTest extends ApiTester
{

    /** @test */
    function system_init()
    {
        fwrite(STDOUT, __METHOD__ . "\n");
        self::createEM(); //this has all the code to init the system...
    }
}
0 của mình, bạn có thể muốn
php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

final class TemplateMethodsTest extends TestCase
{
    public static function setUpBeforeClass(): void
    {
        fwrite(STDOUT, __METHOD__ . "\n");
    }

    protected function setUp(): void
    {
        fwrite(STDOUT, __METHOD__ . "\n");
    }

    protected function assertPreConditions(): void
    {
        fwrite(STDOUT, __METHOD__ . "\n");
    }

    public function testOne(): void
    {
        fwrite(STDOUT, __METHOD__ . "\n");
        $this->assertTrue(true);
    }

    public function testTwo(): void
    {
        fwrite(STDOUT, __METHOD__ . "\n");
        $this->assertTrue(false);
    }

    protected function assertPostConditions(): void
    {
        fwrite(STDOUT, __METHOD__ . "\n");
    }

    protected function tearDown(): void
    {
        fwrite(STDOUT, __METHOD__ . "\n");
    }

    public static function tearDownAfterClass(): void
    {
        fwrite(STDOUT, __METHOD__ . "\n");
    }

    protected function onNotSuccessfulTest(Throwable $t): void
    {
        fwrite(STDOUT, __METHOD__ . "\n");
        throw $t;
    }
}
2 các biến giữ các đối tượng đó trong
class SystemSetupTest extends ApiTester
{

    /** @test */
    function system_init()
    {
        fwrite(STDOUT, __METHOD__ . "\n");
        self::createEM(); //this has all the code to init the system...
    }
}
2 của bạn để chúng có thể được thu thập rác sớm hơn. Các đối tượng được tạo trong ____10 (hoặc phương pháp thử nghiệm) được lưu trữ trong các thuộc tính của đối tượng thử nghiệm chỉ tự động được thu thập ở cuối quy trình PHP chạy PHPUNIT.

Biến thể

Điều gì xảy ra khi bạn có hai thử nghiệm với các thiết lập hơi khác nhau? Có hai khả năng:

  • Nếu mã
    class SystemSetupTest extends ApiTester
    {
    
        /** @test */
        function system_init()
        {
            fwrite(STDOUT, __METHOD__ . "\n");
            self::createEM(); //this has all the code to init the system...
        }
    }
    
    0 chỉ khác nhau một chút, hãy di chuyển mã khác với mã
    class SystemSetupTest extends ApiTester
    {
    
        /** @test */
        function system_init()
        {
            fwrite(STDOUT, __METHOD__ . "\n");
            self::createEM(); //this has all the code to init the system...
        }
    }
    
    0 sang phương thức kiểm tra.
  • Nếu bạn thực sự có một ____10 khác, bạn cần một lớp trường hợp kiểm tra khác. Đặt tên cho lớp sau sự khác biệt trong thiết lập.

Chia sẻ trận đấu

Có một vài lý do chính đáng để chia sẻ đồ đạc giữa các thử nghiệm, nhưng trong hầu hết các trường hợp, cần phải chia sẻ một vật cố giữa các thử nghiệm bắt nguồn từ một vấn đề thiết kế chưa được giải quyết.

Một ví dụ điển hình về một trận đấu có ý nghĩa để chia sẻ qua một số thử nghiệm là kết nối cơ sở dữ liệu: bạn đăng nhập vào cơ sở dữ liệu một lần và sử dụng lại kết nối cơ sở dữ liệu thay vì tạo kết nối mới cho mỗi thử nghiệm. Điều này làm cho các bài kiểm tra của bạn chạy nhanh hơn.

Ví dụ 4.3 sử dụng các phương thức mẫu

php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

final class StackTest extends TestCase
{
    private $stack;

    protected function setUp(): void
    {
        $this->stack = [];
    }

    public function testEmpty(): void
    {
        $this->assertTrue(empty($this->stack));
    }

    public function testPush(): void
    {
        array_push($this->stack, 'foo');

        $this->assertSame('foo', $this->stack[count($this->stack)-1]);
        $this->assertFalse(empty($this->stack));
    }

    public function testPop(): void
    {
        array_push($this->stack, 'foo');

        $this->assertSame('foo', array_pop($this->stack));
        $this->assertTrue(empty($this->stack));
    }
}
3 và
php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

final class StackTest extends TestCase
{
    private $stack;

    protected function setUp(): void
    {
        $this->stack = [];
    }

    public function testEmpty(): void
    {
        $this->assertTrue(empty($this->stack));
    }

    public function testPush(): void
    {
        array_push($this->stack, 'foo');

        $this->assertSame('foo', $this->stack[count($this->stack)-1]);
        $this->assertFalse(empty($this->stack));
    }

    public function testPop(): void
    {
        array_push($this->stack, 'foo');

        $this->assertSame('foo', array_pop($this->stack));
        $this->assertTrue(empty($this->stack));
    }
}
4 để kết nối với cơ sở dữ liệu trước khi thử nghiệm trường hợp thử nghiệm thử nghiệm đầu tiên và ngắt kết nối với cơ sở dữ liệu sau lần thử nghiệm cuối cùng của trường hợp thử nghiệm. uses the
php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

final class StackTest extends TestCase
{
    private $stack;

    protected function setUp(): void
    {
        $this->stack = [];
    }

    public function testEmpty(): void
    {
        $this->assertTrue(empty($this->stack));
    }

    public function testPush(): void
    {
        array_push($this->stack, 'foo');

        $this->assertSame('foo', $this->stack[count($this->stack)-1]);
        $this->assertFalse(empty($this->stack));
    }

    public function testPop(): void
    {
        array_push($this->stack, 'foo');

        $this->assertSame('foo', array_pop($this->stack));
        $this->assertTrue(empty($this->stack));
    }
}
3 and
php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

final class StackTest extends TestCase
{
    private $stack;

    protected function setUp(): void
    {
        $this->stack = [];
    }

    public function testEmpty(): void
    {
        $this->assertTrue(empty($this->stack));
    }

    public function testPush(): void
    {
        array_push($this->stack, 'foo');

        $this->assertSame('foo', $this->stack[count($this->stack)-1]);
        $this->assertFalse(empty($this->stack));
    }

    public function testPop(): void
    {
        array_push($this->stack, 'foo');

        $this->assertSame('foo', array_pop($this->stack));
        $this->assertTrue(empty($this->stack));
    }
}
4 template methods to connect to the database before the test case class’ first test and to disconnect from the database after the last test of the test case, respectively.

Ví dụ 4.3 Chia sẻ trận đấu giữa các bài kiểm tra của bộ kiểm traSharing fixture between the tests of a test suite

php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

final class DatabaseTest extends TestCase
{
    private static $dbh;

    public static function setUpBeforeClass(): void
    {
        self::$dbh = new PDO('sqlite::memory:');
    }

    public static function tearDownAfterClass(): void
    {
        self::$dbh = null;
    }
}

Không thể nhấn mạnh đủ để chia sẻ đồ đạc giữa các thử nghiệm làm giảm giá trị của các bài kiểm tra. Vấn đề thiết kế cơ bản là các đối tượng không được ghép nối một cách lỏng lẻo. Bạn sẽ đạt được kết quả tốt hơn để giải quyết vấn đề thiết kế cơ bản và sau đó viết các bài kiểm tra bằng cách sử dụng các sơ khai (xem bài kiểm tra nhân đôi), hơn là bằng cách tạo các phụ thuộc giữa các bài kiểm tra trong thời gian chạy và bỏ qua cơ hội để cải thiện thiết kế của bạn.Test Doubles), than by creating dependencies between tests at runtime and ignoring the opportunity to improve your design.

Nhà nước toàn cầu Or

Thật khó để kiểm tra mã sử dụng singletons. Điều tương tự cũng đúng với mã sử dụng các biến toàn cầu. Thông thường, mã bạn muốn kiểm tra được kết hợp mạnh mẽ với một biến toàn cầu và bạn không thể kiểm soát việc tạo của nó. Một vấn đề bổ sung là thực tế là một thử nghiệm thay đổi thành biến toàn cầu có thể phá vỡ một thử nghiệm khác.

Trong PHP, các biến toàn cầu hoạt động như thế này:

  • Một biến toàn cầu
    $ phpunit TemplateMethodsTest
    PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
    
    TemplateMethodsTest::setUpBeforeClass
    TemplateMethodsTest::setUp
    TemplateMethodsTest::assertPreConditions
    TemplateMethodsTest::testOne
    TemplateMethodsTest::assertPostConditions
    TemplateMethodsTest::tearDown
    .TemplateMethodsTest::setUp
    TemplateMethodsTest::assertPreConditions
    TemplateMethodsTest::testTwo
    TemplateMethodsTest::tearDown
    TemplateMethodsTest::onNotSuccessfulTest
    FTemplateMethodsTest::tearDownAfterClass
    
    Time: 0 seconds, Memory: 5.25Mb
    
    There was 1 failure:
    
    1) TemplateMethodsTest::testTwo
    Failed asserting that  is true.
    /home/sb/TemplateMethodsTest.php:30
    
    FAILURES!
    Tests: 2, Assertions: 2, Failures: 1.
    
    0 được lưu trữ dưới dạng
    $ phpunit TemplateMethodsTest
    PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
    
    TemplateMethodsTest::setUpBeforeClass
    TemplateMethodsTest::setUp
    TemplateMethodsTest::assertPreConditions
    TemplateMethodsTest::testOne
    TemplateMethodsTest::assertPostConditions
    TemplateMethodsTest::tearDown
    .TemplateMethodsTest::setUp
    TemplateMethodsTest::assertPreConditions
    TemplateMethodsTest::testTwo
    TemplateMethodsTest::tearDown
    TemplateMethodsTest::onNotSuccessfulTest
    FTemplateMethodsTest::tearDownAfterClass
    
    Time: 0 seconds, Memory: 5.25Mb
    
    There was 1 failure:
    
    1) TemplateMethodsTest::testTwo
    Failed asserting that  is true.
    /home/sb/TemplateMethodsTest.php:30
    
    FAILURES!
    Tests: 2, Assertions: 2, Failures: 1.
    
    1.
  • Biến
    $ phpunit TemplateMethodsTest
    PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
    
    TemplateMethodsTest::setUpBeforeClass
    TemplateMethodsTest::setUp
    TemplateMethodsTest::assertPreConditions
    TemplateMethodsTest::testOne
    TemplateMethodsTest::assertPostConditions
    TemplateMethodsTest::tearDown
    .TemplateMethodsTest::setUp
    TemplateMethodsTest::assertPreConditions
    TemplateMethodsTest::testTwo
    TemplateMethodsTest::tearDown
    TemplateMethodsTest::onNotSuccessfulTest
    FTemplateMethodsTest::tearDownAfterClass
    
    Time: 0 seconds, Memory: 5.25Mb
    
    There was 1 failure:
    
    1) TemplateMethodsTest::testTwo
    Failed asserting that  is true.
    /home/sb/TemplateMethodsTest.php:30
    
    FAILURES!
    Tests: 2, Assertions: 2, Failures: 1.
    
    2 là một biến được gọi là biến siêu toàn cầu.
  • Các biến siêu toàn cầu là các biến tích hợp luôn có sẵn trong tất cả các phạm vi.
  • Trong phạm vi của một hàm hoặc phương thức, bạn có thể truy cập biến toàn cầu
    $ phpunit TemplateMethodsTest
    PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
    
    TemplateMethodsTest::setUpBeforeClass
    TemplateMethodsTest::setUp
    TemplateMethodsTest::assertPreConditions
    TemplateMethodsTest::testOne
    TemplateMethodsTest::assertPostConditions
    TemplateMethodsTest::tearDown
    .TemplateMethodsTest::setUp
    TemplateMethodsTest::assertPreConditions
    TemplateMethodsTest::testTwo
    TemplateMethodsTest::tearDown
    TemplateMethodsTest::onNotSuccessfulTest
    FTemplateMethodsTest::tearDownAfterClass
    
    Time: 0 seconds, Memory: 5.25Mb
    
    There was 1 failure:
    
    1) TemplateMethodsTest::testTwo
    Failed asserting that  is true.
    /home/sb/TemplateMethodsTest.php:30
    
    FAILURES!
    Tests: 2, Assertions: 2, Failures: 1.
    
    3 bằng cách truy cập trực tiếp
    $ phpunit TemplateMethodsTest
    PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
    
    TemplateMethodsTest::setUpBeforeClass
    TemplateMethodsTest::setUp
    TemplateMethodsTest::assertPreConditions
    TemplateMethodsTest::testOne
    TemplateMethodsTest::assertPostConditions
    TemplateMethodsTest::tearDown
    .TemplateMethodsTest::setUp
    TemplateMethodsTest::assertPreConditions
    TemplateMethodsTest::testTwo
    TemplateMethodsTest::tearDown
    TemplateMethodsTest::onNotSuccessfulTest
    FTemplateMethodsTest::tearDownAfterClass
    
    Time: 0 seconds, Memory: 5.25Mb
    
    There was 1 failure:
    
    1) TemplateMethodsTest::testTwo
    Failed asserting that  is true.
    /home/sb/TemplateMethodsTest.php:30
    
    FAILURES!
    Tests: 2, Assertions: 2, Failures: 1.
    
    4 hoặc bằng cách sử dụng
    $ phpunit TemplateMethodsTest
    PHPUnit 9.5.0 by Sebastian Bergmann and contributors.
    
    TemplateMethodsTest::setUpBeforeClass
    TemplateMethodsTest::setUp
    TemplateMethodsTest::assertPreConditions
    TemplateMethodsTest::testOne
    TemplateMethodsTest::assertPostConditions
    TemplateMethodsTest::tearDown
    .TemplateMethodsTest::setUp
    TemplateMethodsTest::assertPreConditions
    TemplateMethodsTest::testTwo
    TemplateMethodsTest::tearDown
    TemplateMethodsTest::onNotSuccessfulTest
    FTemplateMethodsTest::tearDownAfterClass
    
    Time: 0 seconds, Memory: 5.25Mb
    
    There was 1 failure:
    
    1) TemplateMethodsTest::testTwo
    Failed asserting that  is true.
    /home/sb/TemplateMethodsTest.php:30
    
    FAILURES!
    Tests: 2, Assertions: 2, Failures: 1.
    
    5 để tạo biến cục bộ có tham chiếu đến biến toàn cầu.

Bên cạnh các biến toàn cầu, các thuộc tính tĩnh của các lớp cũng là một phần của trạng thái toàn cầu.

Trước phiên bản 6, theo mặc định, PHPUNIT đã thực hiện các bài kiểm tra của bạn theo cách thay đổi các biến toàn cầu và siêu toàn cầu (

$ phpunit TemplateMethodsTest
PHPUnit 9.5.0 by Sebastian Bergmann and contributors.

TemplateMethodsTest::setUpBeforeClass
TemplateMethodsTest::setUp
TemplateMethodsTest::assertPreConditions
TemplateMethodsTest::testOne
TemplateMethodsTest::assertPostConditions
TemplateMethodsTest::tearDown
.TemplateMethodsTest::setUp
TemplateMethodsTest::assertPreConditions
TemplateMethodsTest::testTwo
TemplateMethodsTest::tearDown
TemplateMethodsTest::onNotSuccessfulTest
FTemplateMethodsTest::tearDownAfterClass

Time: 0 seconds, Memory: 5.25Mb

There was 1 failure:

1) TemplateMethodsTest::testTwo
Failed asserting that  is true.
/home/sb/TemplateMethodsTest.php:30

FAILURES!
Tests: 2, Assertions: 2, Failures: 1.
2,
$ phpunit TemplateMethodsTest
PHPUnit 9.5.0 by Sebastian Bergmann and contributors.

TemplateMethodsTest::setUpBeforeClass
TemplateMethodsTest::setUp
TemplateMethodsTest::assertPreConditions
TemplateMethodsTest::testOne
TemplateMethodsTest::assertPostConditions
TemplateMethodsTest::tearDown
.TemplateMethodsTest::setUp
TemplateMethodsTest::assertPreConditions
TemplateMethodsTest::testTwo
TemplateMethodsTest::tearDown
TemplateMethodsTest::onNotSuccessfulTest
FTemplateMethodsTest::tearDownAfterClass

Time: 0 seconds, Memory: 5.25Mb

There was 1 failure:

1) TemplateMethodsTest::testTwo
Failed asserting that  is true.
/home/sb/TemplateMethodsTest.php:30

FAILURES!
Tests: 2, Assertions: 2, Failures: 1.
7,
$ phpunit TemplateMethodsTest
PHPUnit 9.5.0 by Sebastian Bergmann and contributors.

TemplateMethodsTest::setUpBeforeClass
TemplateMethodsTest::setUp
TemplateMethodsTest::assertPreConditions
TemplateMethodsTest::testOne
TemplateMethodsTest::assertPostConditions
TemplateMethodsTest::tearDown
.TemplateMethodsTest::setUp
TemplateMethodsTest::assertPreConditions
TemplateMethodsTest::testTwo
TemplateMethodsTest::tearDown
TemplateMethodsTest::onNotSuccessfulTest
FTemplateMethodsTest::tearDownAfterClass

Time: 0 seconds, Memory: 5.25Mb

There was 1 failure:

1) TemplateMethodsTest::testTwo
Failed asserting that  is true.
/home/sb/TemplateMethodsTest.php:30

FAILURES!
Tests: 2, Assertions: 2, Failures: 1.
8,
$ phpunit TemplateMethodsTest
PHPUnit 9.5.0 by Sebastian Bergmann and contributors.

TemplateMethodsTest::setUpBeforeClass
TemplateMethodsTest::setUp
TemplateMethodsTest::assertPreConditions
TemplateMethodsTest::testOne
TemplateMethodsTest::assertPostConditions
TemplateMethodsTest::tearDown
.TemplateMethodsTest::setUp
TemplateMethodsTest::assertPreConditions
TemplateMethodsTest::testTwo
TemplateMethodsTest::tearDown
TemplateMethodsTest::onNotSuccessfulTest
FTemplateMethodsTest::tearDownAfterClass

Time: 0 seconds, Memory: 5.25Mb

There was 1 failure:

1) TemplateMethodsTest::testTwo
Failed asserting that  is true.
/home/sb/TemplateMethodsTest.php:30

FAILURES!
Tests: 2, Assertions: 2, Failures: 1.
9,
php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

final class DatabaseTest extends TestCase
{
    private static $dbh;

    public static function setUpBeforeClass(): void
    {
        self::$dbh = new PDO('sqlite::memory:');
    }

    public static function tearDownAfterClass(): void
    {
        self::$dbh = null;
    }
}
0,
php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

final class DatabaseTest extends TestCase
{
    private static $dbh;

    public static function setUpBeforeClass(): void
    {
        self::$dbh = new PDO('sqlite::memory:');
    }

    public static function tearDownAfterClass(): void
    {
        self::$dbh = null;
    }
}
1,
php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

final class DatabaseTest extends TestCase
{
    private static $dbh;

    public static function setUpBeforeClass(): void
    {
        self::$dbh = new PDO('sqlite::memory:');
    }

    public static function tearDownAfterClass(): void
    {
        self::$dbh = null;
    }
}
2,
php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

final class DatabaseTest extends TestCase
{
    private static $dbh;

    public static function setUpBeforeClass(): void
    {
        self::$dbh = new PDO('sqlite::memory:');
    }

    public static function tearDownAfterClass(): void
    {
        self::$dbh = null;
    }
}
3) không ảnh hưởng đến các thử nghiệm khác.

Kể từ phiên bản 6, PHPUNIT không thực hiện hoạt động sao lưu và khôi phục này cho các biến toàn cầu và siêu toàn cầu theo mặc định nữa. Nó có thể được kích hoạt bằng cách sử dụng tùy chọn

php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

final class DatabaseTest extends TestCase
{
    private static $dbh;

    public static function setUpBeforeClass(): void
    {
        self::$dbh = new PDO('sqlite::memory:');
    }

    public static function tearDownAfterClass(): void
    {
        self::$dbh = null;
    }
}
4 hoặc cài đặt
php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

final class DatabaseTest extends TestCase
{
    private static $dbh;

    public static function setUpBeforeClass(): void
    {
        self::$dbh = new PDO('sqlite::memory:');
    }

    public static function tearDownAfterClass(): void
    {
        self::$dbh = null;
    }
}
5 trong tệp cấu hình XML.

Bằng cách sử dụng tùy chọn

php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

final class DatabaseTest extends TestCase
{
    private static $dbh;

    public static function setUpBeforeClass(): void
    {
        self::$dbh = new PDO('sqlite::memory:');
    }

    public static function tearDownAfterClass(): void
    {
        self::$dbh = null;
    }
}
6 hoặc cài đặt
php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

final class DatabaseTest extends TestCase
{
    private static $dbh;

    public static function setUpBeforeClass(): void
    {
        self::$dbh = new PDO('sqlite::memory:');
    }

    public static function tearDownAfterClass(): void
    {
        self::$dbh = null;
    }
}
7 trong tệp cấu hình XML, sự cô lập này có thể được mở rộng thành các thuộc tính tĩnh của các lớp.

Ghi chú

Các hoạt động sao lưu và khôi phục cho các biến toàn cầu và các thuộc tính lớp tĩnh sử dụng

php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

final class DatabaseTest extends TestCase
{
    private static $dbh;

    public static function setUpBeforeClass(): void
    {
        self::$dbh = new PDO('sqlite::memory:');
    }

    public static function tearDownAfterClass(): void
    {
        self::$dbh = null;
    }
}
8 và
php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

final class DatabaseTest extends TestCase
{
    private static $dbh;

    public static function setUpBeforeClass(): void
    {
        self::$dbh = new PDO('sqlite::memory:');
    }

    public static function tearDownAfterClass(): void
    {
        self::$dbh = null;
    }
}
9.

Các đối tượng của một số lớp (ví dụ:

final class MyTest extends TestCase
{
    protected $backupGlobalsExcludeList = ['globalVariable'];

    // ...
}
0) không thể được tuần tự hóa và hoạt động sao lưu sẽ bị phá vỡ khi một đối tượng như vậy được lưu trữ, ví dụ: Trong mảng
$ phpunit TemplateMethodsTest
PHPUnit 9.5.0 by Sebastian Bergmann and contributors.

TemplateMethodsTest::setUpBeforeClass
TemplateMethodsTest::setUp
TemplateMethodsTest::assertPreConditions
TemplateMethodsTest::testOne
TemplateMethodsTest::assertPostConditions
TemplateMethodsTest::tearDown
.TemplateMethodsTest::setUp
TemplateMethodsTest::assertPreConditions
TemplateMethodsTest::testTwo
TemplateMethodsTest::tearDown
TemplateMethodsTest::onNotSuccessfulTest
FTemplateMethodsTest::tearDownAfterClass

Time: 0 seconds, Memory: 5.25Mb

There was 1 failure:

1) TemplateMethodsTest::testTwo
Failed asserting that  is true.
/home/sb/TemplateMethodsTest.php:30

FAILURES!
Tests: 2, Assertions: 2, Failures: 1.
2.

Chú thích

final class MyTest extends TestCase
{
    protected $backupGlobalsExcludeList = ['globalVariable'];

    // ...
}
2 được thảo luận trong @backupglobals có thể được sử dụng để kiểm soát các hoạt động sao lưu và khôi phục cho các biến toàn cầu. Ngoài ra, bạn có thể cung cấp một danh sách các biến toàn cầu sẽ được loại trừ khỏi các hoạt động sao lưu và khôi phục như thế này@backupGlobals can be used to control the backup and restore operations for global variables. Alternatively, you can provide a list of global variables that are to be excluded from the backup and restore operations like this

final class MyTest extends TestCase
{
    protected $backupGlobalsExcludeList = ['globalVariable'];

    // ...
}

Ghi chú

Các hoạt động sao lưu và khôi phục cho các biến toàn cầu và các thuộc tính lớp tĩnh sử dụng

php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

final class DatabaseTest extends TestCase
{
    private static $dbh;

    public static function setUpBeforeClass(): void
    {
        self::$dbh = new PDO('sqlite::memory:');
    }

    public static function tearDownAfterClass(): void
    {
        self::$dbh = null;
    }
}
8 và
php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

final class DatabaseTest extends TestCase
{
    private static $dbh;

    public static function setUpBeforeClass(): void
    {
        self::$dbh = new PDO('sqlite::memory:');
    }

    public static function tearDownAfterClass(): void
    {
        self::$dbh = null;
    }
}
9.

Các đối tượng của một số lớp (ví dụ:

final class MyTest extends TestCase
{
    protected $backupGlobalsExcludeList = ['globalVariable'];

    // ...
}
0) không thể được tuần tự hóa và hoạt động sao lưu sẽ bị phá vỡ khi một đối tượng như vậy được lưu trữ, ví dụ: Trong mảng
$ phpunit TemplateMethodsTest
PHPUnit 9.5.0 by Sebastian Bergmann and contributors.

TemplateMethodsTest::setUpBeforeClass
TemplateMethodsTest::setUp
TemplateMethodsTest::assertPreConditions
TemplateMethodsTest::testOne
TemplateMethodsTest::assertPostConditions
TemplateMethodsTest::tearDown
.TemplateMethodsTest::setUp
TemplateMethodsTest::assertPreConditions
TemplateMethodsTest::testTwo
TemplateMethodsTest::tearDown
TemplateMethodsTest::onNotSuccessfulTest
FTemplateMethodsTest::tearDownAfterClass

Time: 0 seconds, Memory: 5.25Mb

There was 1 failure:

1) TemplateMethodsTest::testTwo
Failed asserting that  is true.
/home/sb/TemplateMethodsTest.php:30

FAILURES!
Tests: 2, Assertions: 2, Failures: 1.
2.@backupStaticAttributes can be used to back up all static property values in all declared classes before each test and restore them afterwards.

Chú thích

final class MyTest extends TestCase
{
    protected $backupGlobalsExcludeList = ['globalVariable'];

    // ...
}
2 được thảo luận trong @backupglobals có thể được sử dụng để kiểm soát các hoạt động sao lưu và khôi phục cho các biến toàn cầu. Ngoài ra, bạn có thể cung cấp một danh sách các biến toàn cầu sẽ được loại trừ khỏi các hoạt động sao lưu và khôi phục như thế này

Ghi chú

Các hoạt động sao lưu và khôi phục cho các biến toàn cầu và các thuộc tính lớp tĩnh sử dụng

php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

final class DatabaseTest extends TestCase
{
    private static $dbh;

    public static function setUpBeforeClass(): void
    {
        self::$dbh = new PDO('sqlite::memory:');
    }

    public static function tearDownAfterClass(): void
    {
        self::$dbh = null;
    }
}
8 và
php declare(strict_types=1);
use PHPUnit\Framework\TestCase;

final class DatabaseTest extends TestCase
{
    private static $dbh;

    public static function setUpBeforeClass(): void
    {
        self::$dbh = new PDO('sqlite::memory:');
    }

    public static function tearDownAfterClass(): void
    {
        self::$dbh = null;
    }
}
9.

Các đối tượng của một số lớp (ví dụ:

final class MyTest extends TestCase
{
    protected $backupGlobalsExcludeList = ['globalVariable'];

    // ...
}
0) không thể được tuần tự hóa và hoạt động sao lưu sẽ bị phá vỡ khi một đối tượng như vậy được lưu trữ, ví dụ: Trong mảng
$ phpunit TemplateMethodsTest
PHPUnit 9.5.0 by Sebastian Bergmann and contributors.

TemplateMethodsTest::setUpBeforeClass
TemplateMethodsTest::setUp
TemplateMethodsTest::assertPreConditions
TemplateMethodsTest::testOne
TemplateMethodsTest::assertPostConditions
TemplateMethodsTest::tearDown
.TemplateMethodsTest::setUp
TemplateMethodsTest::assertPreConditions
TemplateMethodsTest::testTwo
TemplateMethodsTest::tearDown
TemplateMethodsTest::onNotSuccessfulTest
FTemplateMethodsTest::tearDownAfterClass

Time: 0 seconds, Memory: 5.25Mb

There was 1 failure:

1) TemplateMethodsTest::testTwo
Failed asserting that  is true.
/home/sb/TemplateMethodsTest.php:30

FAILURES!
Tests: 2, Assertions: 2, Failures: 1.
2.

Chú thích

final class MyTest extends TestCase
{
    protected $backupGlobalsExcludeList = ['globalVariable'];

    // ...
}
2 được thảo luận trong @backupglobals có thể được sử dụng để kiểm soát các hoạt động sao lưu và khôi phục cho các biến toàn cầu. Ngoài ra, bạn có thể cung cấp một danh sách các biến toàn cầu sẽ được loại trừ khỏi các hoạt động sao lưu và khôi phục như thế này

Bạn có thể cung cấp một danh sách các thuộc tính tĩnh sẽ được loại trừ khỏi các hoạt động sao lưu và khôi phục:

final class MyTest extends TestCase
{
    protected $backupStaticAttributesExcludeList = [
        'className' => ['attributeName']
    ];

    // ...
}

Ghi chú

Đặt thuộc tính

final class MyTest extends TestCase
{
    protected $backupStaticAttributesExcludeList = [
        'className' => ['attributeName']
    ];

    // ...
}
0 bên trong, ví dụ: Phương pháp
class SystemSetupTest extends ApiTester
{

    /** @test */
    function system_init()
    {
        fwrite(STDOUT, __METHOD__ . "\n");
        self::createEM(); //this has all the code to init the system...
    }
}
0 không có hiệu lực.

Thử nghiệm thử nghiệm là gì?

Một trường hợp kiểm tra Tear Down sẽ thực hiện khi kết thúc lần chạy thử nghiệm trong thư mục thử nghiệm.Các trường hợp thử nghiệm xé được sử dụng để thực hiện các hành động thực thi kiểm tra sau.Ví dụ, một trường hợp thử nghiệm xé nát có thể được sử dụng để xóa dữ liệu thử nghiệm được tạo trong quá trình thực hiện thử nghiệm.used to perform post test execution actions. For example, a teardown test case can be used to delete test data generated during test execution.

Thiết lập có chạy trước mỗi bài kiểm tra không?

Các phương thức mẫu thiết lập () và rehown () được chạy một lần cho mỗi phương thức thử nghiệm (và trên các trường hợp mới) của lớp trường hợp thử nghiệm.run once for each test method (and on fresh instances) of the test case class.

Thuộc tính nào sẽ làm cho một hàm chỉ gọi một lần cho toàn bộ chạy thử?

Thuộc tính này là để xác định các phương thức được gọi một lần trước khi thực hiện bất kỳ thử nghiệm nào trong trận đấu.Nó có thể xuất hiện trên các phương thức kiểm tra hoặc thiết lập.

Là thiết lập JUnit được gọi cho mỗi bài kiểm tra?

Thảo luận.Như đã nêu trong Recipe 4.6, JUnIT Calling Setup () trước mỗi lần kiểm tra và Teckown () sau mỗi lần kiểm tra.Trong một số trường hợp, bạn có thể muốn gọi một phương thức thiết lập đặc biệt một lần trước khi một loạt các thử nghiệm, sau đó gọi một phương thức rehown một lần sau khi tất cả các thử nghiệm hoàn tất.JUnit calls setUp( ) before each test, and tearDown( ) after each test. In some cases you might want to call a special setup method once before a series of tests, and then call a teardown method once after all tests are complete.