跳至主要內容

php单元测试

Moments大约 1 分钟

php单元测试


安装phpunit

// 扩展包
composer require "phpunit/phpunit"
// 创建
php artisan make:test Operations/OperationsTest --unit
// 执行
./vendor/bin/phpunit tests/Unit/Operations/OperationsTest.php
// 执行某个测试函数
./vendor/bin/phpunit --filter testExample

PhpStorm配置phpunit

Languages & Frameworks -> PHP -> Test Frameworks

# Path to phpunit.phar
# Path to phpunit.phar /vendor/bin/phpunit

简单例子

php artisan make:controller OperationsController

app/Http/Controllers/OperationsController.php

public function sum($a, $b)
{
    return $a + $b;
}
  • File -> New -> Php Test -> PHPUnit Test
use FirstExample;
use SecondExample;

private $operations;

/**
 * fixture
 */
public function setUp()
{
    parent::setUp(); // TODO: Change the autogenerated stub

    $this->operations = new OperationsController();
}

/**
 * @return array
 * @test
 */
public function init()
{
    $a = 100;
    $b = 200;

    $this->assertLessThan($a, 0);
    $this->assertLessThan($b, 0);

    return [$a, $b];
}

/**
 * @param $data
 * @test
 * @depends init
 */
public function sum($data)
{
    list($a, $b) = $data;
    $result = $this->operations->sum($a, $b);

    $this->assertEquals($result, 300);
}

接口测试

public function login()
{
    $response = $this->json('POST', 'login', [
        'email'    => 'nobody@qq.com',
        'password' => '123456',
    ]);

    $response->assertStatus(200);
}

数据对比测试

use GuzzleHttp\Client;

$response = $this->json('GET', '/common/permissions?token=11111111');

$response->assertStatus(200);

$client      = new Client();
$r           = $client->request('GET', self::URL . '/common/permissions?token=222222222');
$lineActions = json_decode($r->getBody()->getContents())->response->actions;

$actions     = json_decode($response->getContent())->response->actions;
$actions     = array_unique($actions);
$lineActions = array_unique($lineActions);

sort($actions);
sort($lineActions);

$this->assertEquals($actions, $lineActions);

网页脚本录制工具

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

# driver = webdriver.Firefox()
driver = webdriver.Chrome()
driver.get("http://salon-client-sit.lite.tmp.com/#/")

driver.find_element_by_xpath(u"(.//*[normalize-space(text()) and normalize-space(.)='nobody软件'])[2]/following::input[1]").send_keys("nobody")
driver.find_element_by_xpath(u"(.//*[normalize-space(text()) and normalize-space(.)='nobody软件'])[2]/following::input[2]").send_keys("123456")
driver.find_element_by_xpath(u"(.//*[normalize-space(text()) and normalize-space(.)='nobody软件'])[2]/following::button[1]").click()

driver.implicitly_wait(500)
driver.find_element_by_xpath('//*[@id="app"]/div[1]/div[1]/div[4]/div[2]/div/div[1]/div/p').click()
driver.implicitly_wait(500)
driver.find_element_by_xpath('//*[@id="app"]/div[1]/div[1]/div[2]/div/div/div').click()

time.sleep(2)

driver.close()

相关工具

  • Selenium IDE
  • Katalon Recorder
上次编辑于:
贡献者: Moments