Skip to content

Latest commit

 

History

History
255 lines (187 loc) · 7.69 KB

README.md

File metadata and controls

255 lines (187 loc) · 7.69 KB

Expycted

Build and Test Python Package

Table of Contents

Overview

Expycted is yet another expect pattern implementation.

It is not dependent on any testing framework and can plug into any as it is just an abstraction over assert.

Examples:

from expycted import expect

expect(True).to_not.be_false()                                  # This will succeed

expect([]).to.be_empty()                                        # This will succeed

expect([1,2,3]).to.contain(3)                                   # This will succeed

expect(10).to.equal("10")                                       # This will raise AssertionError

expect(10).to.be("10")                                          # This will succeed

expect.function(int).to_raise(ValueError).when_called_with('a') # This will also succeed

This package was originally written by @petereon, many thanks!

Installation

Expycted can be installed from PyPi by running:

pip install expycted

Alternatively, you can clone the repository and build your own distribution using poetry:

git clone https://github.com/bdsoha/expycted.git
poetry build

Then you can install it using:

pip install ./dist/expycted-<version>-py3-none-any.whl

Matchers

Matchers are used to ensure some conditions are met on an expected value.

Value Matchers

Value matchers can be used in two equivalent ways demonstrated below:

expect.value(10).to.be_greater_than(1)
expect(10).to.be_greater_than(1)

Currently available matchers are:

Equality and Similarity

equal()

Assert that the expected value is equivalent to the actual value using the == operator.

  • Definition: equal(self, value)
  • Alias: be_equal_to(self, value)

be()

Assert that the expected value is the same as the actual value.

  • Definition: be(self, value)
  • Details: Assert any of the following conditions:
    • Assert the expected value's string representation is the same as the actual value's string representation.
    • When provided two objects, assert that have the same attributes.
    • Assert the expected value equals the actual value.

Numeric

be_greater_than()

Assert that the expected value is greater than the actual value using the > operator.

  • Definition: be_greater_than(self, value)
  • Alias: be_greater(self, value)

be_lesser_than()

Assert that the expected value is less than the actual value using the < operator.

  • Definition: be_lesser_than(self, value)
  • Alias:
    • be_lesser(self, value)
    • be_less(self, value)
    • be_less_than(self, value)

be_greater_or_equal_to()

Assert that the expected value is greater than the actual value using the >= operator.

  • Definition: be_greater_or_equal_to(self, value)
  • Alias:
    • be_greater_or_equal(self, value)
    • be_greater_than_or_equal_to(self, value)

be_lesser_or_equal_to()

Assert that the expected value is less than the actual value using the <= operator.

  • Definition: be_lesser_or_equal_to(self, value)
  • Alias:
    • be_lesser_or_equal(self, value)
    • be_less_or_equal(self, value)
    • be_less_than_or_equal_to(self, value)
    • be_lesser_than_or_equal_to(self, value)

be_numeric()

Assert that the expected value is a number or can be parsed as a number from a string representation.

  • Definition: be_numeric(self)
  • Alias: be_a_number(self)

Containment and Emptiness

contain()

Assert the expected value contains a value using the in keyword.

  • Definition: contain(self, value)
  • Alias:
    • have(self, value)
    • include(self, value)

be_contained_in()

Assert the expected value is contained in a value using the in keyword.

  • Definition: be_contained_in(self, value)
  • Alias:
    • be_in(self, value)
    • be_included_in(self, value)

be_empty()

Assert that the expected value is iterable and False.

  • Definition: be_empty(self)

Truthiness

be_true()

Assert that the expected value is strictly True.

  • Definition: be_true(self)

be_false()

Assert that the expected value is strictly False.

  • Definition: be_false(self)

be_truthy()

Assert that the expected value is equivalent to True.

  • Definition: be_truthy(self)
  • Alias:
    • be_truey(self)
    • be_trueish(self)

be_falsey()

Assert that the expected value is equivalent to False.

  • Definition: be_falsey(self)
  • Alias:
    • be_falsy(self)
    • be_falsish(self)

Typing

be_of_type()

Assert that the expected value has a given type.

  • Definition: be_of_type(self, value)
  • Alias:
    • be_type(self, value)
    • have_type(self, value)

inherit()

Assert that the expected value inherits or is a subclass of a given type.

  • Definition: inherit(self, value)
  • Alias:
    • be_subclass_of(self, value)
    • have_parent(self, value)

Function Matchers

Function matchers can be used as demonstrated below:

expect.function(string.replace).to_return('strength').when_called_with('string', 'ength')

Arguments can be passed to the expected function using the .when_called_with method, (or its alias methods: when_called_with_args and when_called_with_arguments).

to_return()

Assert that the expected function returns the actual value, or type, or both.

  • Definition: to_return(self, value=None, type_of_value=None)

to_raise()

Assert that the expected function raises the actual exception of a given type.

  • Definition: to_raise(self, exception_type)

Filesystem Matchers

Filesystem matchers can be used as demonstrated below:

expect.folder('/some/folder').to.contain('subfolder')

Matchers can be used with both expect.folder('/some/folder').to and expect.folder('/some/folder').to_not to check both positive and negative expectations.

contain()

Assert that the expected folder contains the actual file or folder.

  • Definition: contain(self, name, type: Union[File, Folder, None, str] = None)
  • Details: When the type argument is specified, it will also assert whether actual value is a File or Folder.

contain_file()

Assert that the expected folder contains the actual file.

  • Definition: contain_file(self, name)

contain_folder()

Assert that the expected folder contains the actual folder.

  • Definition: contain_folder(self, name)

exist()

Assert that the expected folder exists.

  • Definition: exist(self)

be_empty()

Assert that the expected folder is empty.

  • Definition: be_empty(self)

Development

For development a combination of poetry and pipenv is used. pipenv is used to install dependencies and manage virtual environments while poetry is used for building and metadata management.

To begin developing run the following commands in your terminal:

# Clone the repository
git clone https://github.com/bdsoha/expycted.git

# Install dependencies
pipenv install

# Run tests
pipenv run test

# Build the package
pipenv run build

Contributing

Project is currently in its infancy, contributors, pull requests and issues are welcome