Skip to content

Instantly share code, notes, and snippets.

@szabacsik
Last active August 19, 2019 08:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save szabacsik/91c514dde683a5f18f95cea8bceaf74f to your computer and use it in GitHub Desktop.
Save szabacsik/91c514dde683a5f18f95cea8bceaf74f to your computer and use it in GitHub Desktop.
Codeception quickstart guide

Codeception quickstart guide

Testing RESTful WebService

Login with the deploy user
su - worker
Go to the project root folder
cd /home/worker/volume/artifact/current
Create a directory for codeception and enter it
mkdir tests/codeception -p; cd ./tests/codeception
Initialize your testing environment with
codecept bootstrap

This command generates codeception.yml global configuration file and tests folder where the test suites (unit, functional, and acceptance) stored.
https://codeception.com/install
https://codeception.com/docs/01-Introduction

codeception.yml has preconfigured settings you can change.
https://codeception.com/docs/reference/Configuration

Suites are independent groups of tests with a common purpose. A “unit suite” for all unit tests, a “functional suite” for all functional tests, and an “acceptance suite” for all acceptance tests.
https://codeception.com/docs/02-GettingStarted
https://codeception.com/docs/01-Introduction

Create new test suite
codecept generate:suite api API_Tester

Requires suite name and optional actor name codecept generate:suite [suite name] [actor name].
Creates Helper ../_support/Helper/Api.php, Actor ../_support/API_Tester.php and Suite config api.suite.yml.
https://codeception.com/docs/02-GettingStarted#Actors

Edit api.suite.yml, enable REST module for this suite
actor: API_Tester
lint: true
modules:
    enabled:
        - \Helper\Api
        - REST:
            url: http://localhost
            depends: PhpBrowser
            part: Json

https://codeception.com/docs/06-ModulesAndHelpers#Standard-Modules
https://codeception.com/docs/modules/REST

Create first test with generate:cest testName ( or test|cept) command
codecept generate:cest api baseUrl

Codeception has its own testing format called Cest (Codecept + Test). To start writing a test we need to create a new Cest file. We can do that by running the following command: codecept generate:cest [suite name] [class name]
creates api/baseUrlCest.php
https://codeception.com/docs/02-GettingStarted#Generators

edit api/baseUrlCest.php
<?php 

class baseUrlCest
{
    public function _before(API_Tester $I)
    {
    }

    // tests
    public function tryToTest(API_Tester $I)
    {
        $I -> sendGET ( '/' );
        $I -> seeResponseCodeIs ( \Codeception\Util\HttpCode::OK );
        $I -> haveHttpHeader ( 'Content-Type', 'application/json' );
        $I -> haveHttpHeader ( 'Content-Type', 'charset=UTF-8' );
        $I -> seeResponseIsJson ();
        $I -> seeResponseMatchesJsonType ( [ 'status' => 'string', 'code' => 'integer' ] );
        $I -> seeResponseJsonMatchesJsonPath ( '$.code' );
        $I -> seeResponseJsonMatchesJsonPath ( '$.status' );
        $I -> seeResponseContainsJson ( [ 'status' => 'ok', 'code' => 200 ] );
    }
}
Run tests
codecept run --debug -vvv

To see all the available options, run the following command:

codecept help run
Add the test files to the git repository of the project
api.suite.yml
api/baseUrlCest.php
Write more test

https://codeception.com/docs/modules/REST

Acceptance Testing

with Chrome Browser through Selenium WebDriver

mkdir test && cd test

Download Selenium Standalone Server

https://docs.seleniumhq.org/download/

curl -sSL http://selenium-release.storage.googleapis.com/3.141/selenium-server-standalone-3.141.59.jar --output selenium-server-standalone-3.141.59.jar

Download ChromeDriver - WebDriver for Chrome

https://sites.google.com/a/chromium.org/chromedriver/downloads

curl -sSL https://chromedriver.storage.googleapis.com/76.0.3809.68/chromedriver_win32.zip --output chromedriver_win32.zip
unzip chromedriver_win32.zip

Download codeception

curl -sSL http://codeception.com/codecept.phar --output codecept.phar

Initialize testing environment

php codecept.phar bootstrap

Create first acceptance test

php codecept.phar generate:cest acceptance First

Set the browser and the url

nano .\tests\acceptance.suite.yml
# Codeception Test Suite Configuration
#
# Suite for acceptance tests.
# Perform tests in browser using the WebDriver or PhpBrowser.
# If you need both WebDriver and PHPBrowser tests - create a separate suite.

actor: AcceptanceTester
modules:
    enabled:
        - WebDriver:
            url: 'https://gist.github.com/szabacsik/91c514dde683a5f18f95cea8bceaf74f'
            browser: chrome
        - \Helper\Acceptance
    step_decorators: ~

Create tests

nano .\tests\acceptance\FirstCest.php
<?php

class FirstCest
{
    public function _before(AcceptanceTester $I)
    {
    }

    // tests
    public function tryToTest(AcceptanceTester $I)
    {
        $I->amOnPage('/');
        $I->see('Codeception quickstart guide');
        $I->click('Back to GitHub');
        $I->see('Repositories');
    }
}

Launch the Selenium Server

java -Dwebdriver.gecko.driver=chromedriver.exe -jar selenium-server-standalone-3.141.59.jar

Run the test

php codecept.phar run --steps

https://codeception.com/docs/03-AcceptanceTests

https://codeception.com/docs/modules/WebDriver#Selenium

https://codeception.com/quickstart

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment