亚洲中字慕日产2020,大陆极品少妇内射AAAAAA,无码av大香线蕉伊人久久,久久精品国产亚洲av麻豆网站

資訊專欄INFORMATION COLUMN

自動化測試:六個值得參考的 Laravel 開源項(xiàng)目

孫吉亮 / 1496人閱讀

摘要:最近我對自動化測試越來越感興趣密切關(guān)注著,,,以及其他測試相關(guān)的關(guān)鍵詞,所以我決定看看周圍其他人是如何做自動化測試的。使用功能測試和組件測試和單元測試差不多。的所有測試包括單元測試功能測試插件測試每個區(qū)域都有對應(yīng)的基類來擴(kuò)展有,和。


最近我對自動化測試越來越感興趣 —— 密切關(guān)注著 PHPUnit,TDD,Laravel Dusk,Travis 以及其他測試相關(guān)的關(guān)鍵詞,所以我決定看看周圍其他人是如何做自動化測試的。我在 GitHub 上找到了相當(dāng)多的 Laravel 開源項(xiàng)目,從里面挑選出了 6 個來查看他們的測試方案。讓我們分別看一下。

免責(zé)聲明: 我只是查看了完整的基于 Laravel 5.3+ 的 Laravel 項(xiàng)目(不包括依賴包)

1. Laravel.io portal

URL:?https://github.com/laravelio/...

最近重新啟動的?Laravel.io 已經(jīng)將代碼在 GitHub 上開源。Dries Vints?在這個項(xiàng)目中寫的測試非常好。

Laravel.io 使用功能測試 (Feature testing) 和組件測試 (Component testing)(和單元測試差不多)。有趣的是,在這兩種測試中都進(jìn)行了相同或相似的測試。

例子 1 -- tests/Feature/ReplyTest.php

public function users_can_add_a_reply_to_a_thread()
{
    factory(Thread::class)->create(["subject" => "The first thread", "slug" => "the-first-thread"]);
    $this->login();
    $this->visit("/forum/the-first-thread")
        ->type("The first reply", "body")
        ->press("Reply")
        ->see("The first thread")
        ->see("The first reply")
        ->see("Reply successfully added!");
}

例子 2 -- tests/Components/Jobs/CreateReplyTest.php

public function we_can_create_a_reply()
{
    $job = new CreateReply("Foo", "", $this->createUser(), factory(Thread::class)->create());
    $this->assertInstanceOf(Reply::class, $job->handle());
}

這樣做很好: 同時測試?Jobs?層和實(shí)際在瀏覽器中點(diǎn)擊一些東西。

我還注意到 Laravel.io 已經(jīng)升級到了 Laravel 5.4, 但是測試套件仍然使用的是5.3的風(fēng)格, 使用?BrowserKitTestCase implementation。 這沒有什么問題,僅僅是一個提醒。

這個項(xiàng)目也使用了 Travis 進(jìn)行持續(xù)集成, 后來我發(fā)現(xiàn)大多數(shù)項(xiàng)目都使用了它。

2. Cachet —— 一個開源狀態(tài)頁面系統(tǒng)

URL:?https://github.com/cachethq/C...

在 James Brooks?和?Graham Campbell 的帶領(lǐng)下,這個項(xiàng)目有一個龐大的測試組件。他甚至通過觀察表層很難理解。

所以,我們從哪里開始... 事實(shí)上,我甚至不會深度燕郊這個項(xiàng)目的測試邏輯, 因?yàn)樗y理解了,這是一個例子 ——?tests/Models/ComponentTest.php

use AltThreeTestBenchValidationTrait;
use CachetHQCachetModelsComponent;
use CachetHQTestsCachetAbstractTestCase;

class ComponentTest extends AbstractTestCase
{
    use ValidationTrait;
    public function testValidation()
    {
        $this->checkRules(new Component());
    }
}

好吧,這里用到了?ValidationTrait,然后是一些?AbstractTestCase。同時這段邏輯是所有的測試 —— 一些抽象的 "魔術(shù)" 正在執(zhí)行所有的工作。

我不是說這是壞事 —— 十分確定他在內(nèi)在的東西里工作的很好。他只是不容易先學(xué)習(xí)和遵循。但如果有人想深入研究 —— 祝好運(yùn)!

3. October CMS

URL:?https://github.com/octobercms...

市場上第一款基于 Laravel 的 CMS,他擁有非常不錯的測試組件。

首先 -——?tests?文件夾有一個?真正信息詳實(shí)的?readme.md?文件,專門用于測試過程。

October CMS 的所有測試包括:

單元測試

功能測試

插件測試

每個 "區(qū)域" 都有對應(yīng)的基類來擴(kuò)展 —— 有?TestCaseUiTestCase?和?PluginTestCase。

邏輯也非常復(fù)雜和抽象 —— 這里有一個例子?tests/unit/backend/models/ExportModelTest.php

class ExportModelTest extends TestCase
{
    //
    // 輔助
    //
    protected static function callProtectedMethod($object, $name, $params = [])
    {
        $className = get_class($object);
        $class = new ReflectionClass($className);
        $method = $class->getMethod($name);
        $method->setAccessible(true);
        return $method->invokeArgs($object, $params);
    }
    //
    // 測試
    //
    public function testEncodeArrayValue()
    {
        $model = new ExampleExportModel;
        $data = ["foo", "bar"];
        $result = self::callProtectedMethod($model, "encodeArrayValue", [$data]);
        $this->assertEquals("foo|bar", $result);
        $data = ["dps | heals | tank", "paladin", "berserker", "gunner"];
        $result = self::callProtectedMethod($model, "encodeArrayValue", [$data]);
        $this->assertEquals("dps | heals | tank|paladin|berserker|gunner", $result);
        $data = ["art direction", "roman empire", "sci-fi"];
        $result = self::callProtectedMethod($model, "encodeArrayValue", [$data, "-"]);
        $this->assertEquals("art direction-roman empire-sci-fi", $result);
    }
}

如你所見,這里有一個靜態(tài)輔助方法(順便說一下,在其他類中重復(fù)使用),然后獲取類/方法并調(diào)用他啊, 我確信作者能立即理解邏輯,但這對外人來說很困難。

同樣有趣的是,OctoberCMS 使用?Selenium?來獲取一些功能:tests/readme.md?文件提到了設(shè)置文檔。

4. Orgmanager —— GitHub 組織的邀請系統(tǒng)

URL:?https://github.com/orgmanager...

這是?Miguel Piedrafita 的一個非常簡單的項(xiàng)目,Orgmanager 的測試也是非常簡單易懂的。還分為單元,功能和 API 測試。

我在這里看到一個有趣的示例 ——?從測試中調(diào)用 Artisan 命令,例如 unit/JoinTest.php

public function testJoinCommand()
{
    $user = factory(User::class)->create();
    $org = factory(Org::class)->create([
      "userid" => $user->id,
    ]);
    Github::shouldReceive("authenticate")
              ->once()
              ->with($org->user->token, null, "http_token")
              ->andReturn();
    Artisan::call("orgmanager:joinorg", [
      "org"      => $org->id,
      "username" => $user->github_username,
    ]);
    $this->assertEquals($user->github_username." was invited to ".$org->name."
", Artisan::output());
}

調(diào)用 artisan 命令并斷言其輸出 —— 非常有趣。我確定他有效,但這是非標(biāo)準(zhǔn)的方式。

5. PHPMap

URL:?https://github.com/PHPMap/phpmap

由 Florian Wartner 創(chuàng)建及維護(hù)。

PHPMap 有一個測試組件,使人聯(lián)想到?Laracasts?或?測試驅(qū)動 Laravel 課程 講述的標(biāo)準(zhǔn)。這是?Feature/FavoritesTest.php 的例子。

public function guests_can_not_favorite_anything()
{
    $this->withExceptionHandling()
        ->post("forum/replies/1/favorites")
        ->assertRedirect("/login");
}

public function an_authenticated_user_can_favorite_any_reply()
{
    $this->signIn();
    $reply = create("AppModelsForumReply");
    $this->post("forum/replies/".$reply->id."/forum/favorites");
    $this->assertCount(1, $reply->favorites);
}

PHPMap 的測試分為單元,功能及?Laravel Dusk 等等!最后我發(fā)現(xiàn)了一個真正在生產(chǎn)環(huán)境使用 Dusk 的項(xiàng)目。這是他的門面 ——?tests/Browser/MapTest.php

public function testMap()
{
    $this->browse(function ($browser) {
        $browser->visit("/map")
                ->assertSee("PHPMap");
    });
}


6. Timegrid —— 免費(fèi),開源,在線操作平臺

URL:?https://github.com/timegridio...

Timegrid 的最大貢獻(xiàn)者是?Ariel Vallese,同時他在測試方面做了非常好的工作。

這里只有很多的測試: 單元,驗(yàn)收和集成,每個文件都有更深的子文件夾目錄,例如:——?acceptance/scenarios/consulting/ConsultingScenarioTest.php

public function it_fits_for_consulting_scenario()
{
    $this->arrangeScenario();
    $this->the_business_publishes_a_consulting_service();
    $this->the_business_publishes_vacancies();
    $this->a_user_subscribes_to_business();
    $this->the_user_queries_vacancies();
    $this->it_provides_available_times_for_requested_service_date();
    $this->the_user_takes_a_reservation();
    $this->the_user_sees_the_reservation_ticket();
}

public function the_business_publishes_a_consulting_service()
{
    $this->service = $this->makeService([
        "name"     => "OnSite 4hs Support",
        "duration" => 60 * 4,
        ]);
    $this->actingAs($this->owner);
    $this->call("POST", route("manager.business.service.store", $this->business), $this->service->toArray());
    $this->assertCount(1, $this->business->fresh()->services);
}

一個一體化的方法,之后是一個個列舉更多的測試:

倉庫中的官方統(tǒng)計(jì)數(shù)據(jù)看起來非常好:?89% 的測試覆蓋率。

最后,有趣的是,作者甚至測試了遷移文件,如?tests/unit/migration/MigrationTest.php:

public function it_refreshes_rollbacks_and_seeds_the_database()
{
    $database = env("DB_CONNECTION");
    $this->assertNotNull($database);
    $exitCode = Artisan::call("migrate:refresh", ["--database" => $database]);
    $this->assertEquals(0, $exitCode);
    $exitCode = Artisan::call("migrate:rollback", ["--database" => $database]);
    $this->assertEquals(0, $exitCode);
    $exitCode = Artisan::call("migrate", ["--database" => $database]);
    $this->assertEquals(0, $exitCode);
    $exitCode = Artisan::call("db:seed", ["--database" => $database]);
    $this->assertEquals(0, $exitCode);
}

在測試中使用 Artisan 命令或許不是最佳的設(shè)計(jì)模式,但他只是測試任何 web 應(yīng)用中最重要的功能之一。

總體結(jié)論

在看過所有這些不同的項(xiàng)目之后(以及由于各種原因未提及的),以下是我對自己關(guān)于測試的主要要求:

不在?單元?"或"?功能?中做選擇, —— 大多數(shù)項(xiàng)目兩者兼具,或者更多類型的測試;

大多數(shù)項(xiàng)目使用持續(xù)集成(通常是 Travis)和測試組件 —— 否則,為什么反感寫測試呢?

這里有非常多的不同方式構(gòu)建測試 —— 這完全取決于項(xiàng)目,這里沒有“高招”;

還有很多方法對內(nèi)部測試功能分組 —— 輔助方法,抽象類,種子數(shù)據(jù)等。沒有具體規(guī)則,找準(zhǔn)適用于你的內(nèi)容。

遷移到較新版本的 Laravel 可能很痛苦 —— 例如,5.3 版本的測試看上去和 5.4 版本不一樣。所以你需要提前考慮更新。

從不同角度考慮 —— 當(dāng)你的項(xiàng)目成長起來,你將不得不回看及修改/添加測試。在這些項(xiàng)目中,我”預(yù)感“有一些遺留代碼,只是因?yàn)橛行y試將不在被使用。

以上是我的經(jīng)驗(yàn),有沒有你要添加到開源項(xiàng)目列表中來學(xué)習(xí)測試的內(nèi)容?

文章轉(zhuǎn)自:https://learnku.com/laravel/t... 

更多文章:https://learnku.com/laravel/c...

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/31068.html

Failed to recv the data from server completely (SIZE:0/8, REASON:closed)