Git Product home page Git Product logo

prove-environment's Introduction

Testing the environment (PHP Behat)

In this project, we're gonna create a simple smoke test for the purpose of proving that the environment we make with Behat is working as it's supposed to be.

So, based on that understanding, this test will going to have the following characteristics:

  • Making sure the environment is working
  • Eliminate dependencies and complexity
  • Do not worry about HTTP, JSON, authentication, Github or anything else yet

Feature file

Our feature will be like this:

Feature: Establish my Environment
	Scenario: I want to prove my environment is working as expected
		Given I have 2 monkeys
		When I get 2 more monkeys
		Then I should have 4 monkeys

This is basically just an arithmetic test to confirm relationship between Given, When, and Then.


Generating the function

Like usual, we generate the function on our workspace by typing command on the terminal:

vendor/bin/behat

Generated function

From that command, we'll get the following functions:

/**
 * @Given I have :arg1 monkeys
 */
public function iHaveMonkeys($arg1)
{
    // stuff
}

/**
 * @When I get :arg1 more monkeys
 */
public function iGetMoreMonkeys($arg1)
{
    // stuff
}

/**
 * @Then I should have :arg1 monkeys
 */
public function iShouldHaveMonkeys($arg1)
{
    // stuff
}

Given: I have 2 monkeys

This generates function iHaveMonkeys($arg1).

The parameter $arg1 took value 2 from the Gherkin code.

Before we could assign from parameter into the variable, we need to declare variable outside all the functions and give it initial value:

protected $monkeyCount = 0;

Now, we could write it like this:

/**
 * @Given I have :arg1 monkeys
 */
public function iHaveMonkeys($arg1)
{
    $this->monkeyCount = (int) $arg1;
}

Notice that we use Integer for the data type, because monkey can't be Float type.

So, in this feature example:

Given I have 2 monkeys

The value 2 will be assigned into $this->monkeyCount.


When: I get 2 more monkeys

This generates function iGetMoreMonkeys($arg1).

Then we'll write the function:

/**
 * @When I get :arg1 more monkeys
 */
public function iGetMoreMonkeys($arg1)
{
    $this->monkeyCount += (int) $arg1;
}

We've already assigned the value 2 into $this->monkeyCount by calling function iHaveMonkeys($arg1).

Now in iGetMoreMonkeys($arg1), we'll take value from $arg1 and add it into already-assigned variable $this->monkeyCount again.

Our feature file:

When I get 2 more monkeys

So, 2 + 2 logically should equals 4


Then: I should have 4 monkeys

This generates function iShouldHaveMonkeys($arg1).

The function we're about to write should be able to assert our arithmetic logic from previously written feature file:

Given I have 2 monkeys
When I get 2 more monkeys
Then I should have 4 monkeys

We need to make sure that sample above should passed the test, and sample below should failed the test:

Given I have 2 monkeys
When I get 2 more monkeys
Then I should have 5 monkeys

So, our function will be like this:

/**
 * @Then I should have :arg1 monkeys
 */
public function iShouldHaveMonkeys($arg1)
{
    Assert::assertEquals($arg1, $this->monkeyCount);
}

The parameter $arg1 here should match the arithmetic result from 2 previous functions which is on variable $this->monkeyCount.

In order to use function assertEquals(), we're gonna use Assert class from PHPUnit, so we need to import it too:

use PHPUnit\Framework\Assert;

Running the test

Run behat from terminal:

vendor/bin/behat

Test result:

Test Passed

Failed test:

Test Failed

This proves that the assertion works properly.


Understand test flow

  • Feature Description
    The feature file describes the scenario of establishing an environment and ensuring it functions correctly.

  • Scenario
    The scenario outlines the steps to verify that adding monkeys to the environment works as expected.

  • Given Step
    Sets up the initial state by specifying the number of monkeys in the environment. When Step: Specifies the action of adding more monkeys to the environment.

  • Then Step
    Asserts that the final state of the environment matches the expected number of monkeys.


What does this test tell us?

  • The test scenario executes successfully, indicating that the environment setup and monkey addition functionalities are working as expected.
  • The assertion in the "Then" step correctly verifies whether the number of monkeys matches the expected count.
  • Changing the expected count in the assertion to see if the test fails demonstrates the test's ability to catch discrepancies.

In summary, this smoke test effectively verifies the basic functionality of managing monkeys in the environment, providing confidence that the system is functioning as intended.


Test demo

Test Demo


License

This repository is licensed under the MIT License - see the LICENSE file for details.

prove-environment's People

Contributors

primprum avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.