Documentation
Base Compare
CompareBase
Bases: ABC
Base class for all comparison classes.
Source code in pytest_compare/base.py
compare(actual)
abstractmethod
Compare two objects.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
actual |
Any
|
The other object to compare to. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the objects are equal, False otherwise. |
Native Compares
CompareIn
Bases: CompareBase
Compare in.
Source code in pytest_compare/native/native.py
__init__(expected, reverse=False)
Initialize the class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
expected |
str
|
The expected value. |
required |
reverse |
bool
|
If True, the actual value is expected to be a substring of the expected value. If False, the expected value is expected to be a substring of the actual value. |
False
|
Source code in pytest_compare/native/native.py
compare(actual)
Compare if the actual value is in the expected value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
actual |
Any
|
The actual value. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the expected value is in the actual value. |
Source code in pytest_compare/native/native.py
CompareIsInstanceOf
Bases: CompareBase
Compare instanceof.
Source code in pytest_compare/native/native.py
compare(actual)
Compare if the actual value is an instance of the expected type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
actual |
Any
|
The actual instance. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the actual instance is an instance of the expected type. |
Source code in pytest_compare/native/native.py
CompareIsSubclassOf
Bases: CompareBase
Compare issubclass.
Source code in pytest_compare/native/native.py
compare(actual)
Compare if the actual value is a subclass of the expected type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
actual |
Any
|
The actual instance. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the actual instance is a subclass of the expected type. |
Source code in pytest_compare/native/native.py
CompareLength
Bases: CompareBase
Compare length.
Source code in pytest_compare/native/native.py
compare(actual)
Compare the actual length to the expected length.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
actual |
Any
|
The actual value. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the actual instance has the expected length. |
Source code in pytest_compare/native/native.py
CompareSubString
Bases: CompareBase
Compare substring.
To test if the actual value is a substring of the expected value, use CompareSubString(expected).
For example:
with patch.object(ProductionClass, 'method', return_value=None) as mock_method:
thing = ProductionClass()
thing.method("actual_string")
# test if mock_method was called with "string" as a substring of the actual value
mock_method.assert_called_once_with(CompareSubString("string"))
# test if the called value is a substring of the expected value
mock_method.assert_called_once_with(CompareSubString("expected_string_containing_actual_string", reverse=True))
# test that the called value is not a substring of the expected value
mock_method.assert_called_once_with(CompareSubString("not_called_value", contains=False))
Source code in pytest_compare/native/native.py
__init__(expected, reverse=False, contains=True)
Initialize the class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
expected |
str
|
The expected value. |
required |
reverse |
bool
|
If True, the actual value is expected to be a substring of the expected value. If False, the expected value is expected to be a substring of the actual value. |
False
|
contains |
bool
|
If True, the expected value is expected to be a substring of the actual value. If False, the expected value is expected to not be a substring of the actual value. |
True
|
Source code in pytest_compare/native/native.py
compare(actual)
Compare if the actual value is a substring of the expected value.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
actual |
str
|
The actual value. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the expected value is a substring of the actual value. |
Source code in pytest_compare/native/native.py
CompareType
Bases: CompareBase
Compare types.
Source code in pytest_compare/native/native.py
compare(actual)
Compare the actual type to the expected type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
actual |
Any
|
The actual value. |
required |
Returns:
Type | Description |
---|---|
bool
|
True if the actual value matches the expected value. |
Source code in pytest_compare/native/native.py
Dict Compares
CompareDictContains
Bases: CompareDictBase
Check if the actual dictionary is a subset of the expected dictionary.
Source code in pytest_compare/dict/dict.py
__init__(expected, reverse_contains=False)
Initialize the class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
expected |
Dict
|
Expected dictionary. |
required |
reverse_contains |
bool
|
If True, the comparison is reversed. |
False
|
Source code in pytest_compare/dict/dict.py
compare(actual)
Check if the actual dictionary is a subset of the expected dictionary. If reverse_contains is True, the comparison is reversed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
actual |
Dict
|
Actual dictionary. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the first dictionary is a subset of the second dictionary, False otherwise. |
Source code in pytest_compare/dict/dict.py
CompareDictKeys
Bases: CompareDictBase
Compare if the actual dictionary has the expected keys.
Source code in pytest_compare/dict/dict.py
compare(actual)
Compare if the actual dictionary has the expected keys.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
actual |
Dict, List[str]
|
Actual dictionary or list of expected keys. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the actual dictionary has the expected keys, False otherwise. |
Source code in pytest_compare/dict/dict.py
Pandas Compares
CompareDataFrame
Bases: CompareDataFrameBase
Compare two dataframes.
Source code in pytest_compare/pandas/pandas.py
__init__(expected, columns=None)
Initialize the class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
expected |
pd.DataFrame
|
Expected Dataframe. |
required |
columns |
List[str]
|
Columns to compare. If None, all columns are compared. Defaults to None. |
None
|
Source code in pytest_compare/pandas/pandas.py
compare(actual)
Compare two dataframes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
actual |
pd.DataFrame
|
Actual Dataframe. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the dataframes are equal, False otherwise. |
Source code in pytest_compare/pandas/pandas.py
CompareDataFrameColumns
Bases: CompareDataFrameBase
Compare two dataframe columns.
Source code in pytest_compare/pandas/pandas.py
compare(actual)
Compare two dataframe columns.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
actual |
pd.DataFrame
|
Actual Dataframe. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if columns are identical, False otherwise. |
Source code in pytest_compare/pandas/pandas.py
CompareSeries
Bases: CompareDataFrameBase
Compare two series.
Source code in pytest_compare/pandas/pandas.py
compare(actual)
Compare two series.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
actual |
pd.Series
|
Actual Series. |
required |
Returns:
Name | Type | Description |
---|---|---|
bool |
bool
|
True if the series are equal, False otherwise. |