On this page
Asserts
Installation
If you use Codeception installed using composer, install this module with the following command:
composer require --dev codeception/module-asserts
Alternatively, you can enable Asserts
module in suite configuration file and run
codecept init upgrade4
This module was bundled with Codeception 2 and 3, but since version 4 it is necessary to install it separately.
Some modules are bundled with PHAR files.
Warning. Using PHAR file and composer in the same project can cause unexpected errors.
Description
Special module for using asserts in your tests.
Actions
assertArrayHasKey
Asserts that an array has a specified key.
param int|string
$keyparam array|ArrayAccess
$arrayparam string
$message
assertArrayNotHasKey
Asserts that an array does not have a specified key.
param int|string
$keyparam array|ArrayAccess
$arrayparam string
$message
assertClassHasAttribute
Asserts that a class has a specified attribute.
param string
$attributeNameparam string
$classNameparam string
$message
assertClassHasStaticAttribute
Asserts that a class has a specified static attribute.
param string
$attributeNameparam string
$classNameparam string
$message
assertClassNotHasAttribute
Asserts that a class does not have a specified attribute.
param string
$attributeNameparam string
$classNameparam string
$message
assertClassNotHasStaticAttribute
Asserts that a class does not have a specified static attribute.
param string
$attributeNameparam string
$classNameparam string
$message
assertContains
Asserts that a haystack contains a needle.
param
$needleparam
$haystackparam string
$message
assertContainsEquals
param
$needleparam
$haystackparam string
$message
assertContainsOnly
Asserts that a haystack contains only values of a given type.
param string
$typeparam
$haystackparam bool|null
$isNativeTypeparam string
$message
assertContainsOnlyInstancesOf
Asserts that a haystack contains only instances of a given class name.
param string
$classNameparam
$haystackparam string
$message
assertCount
Asserts the number of elements of an array, Countable or Traversable.
param int
$expectedCountparam Countable|iterable
$haystackparam string
$message
assertDirectoryDoesNotExist
Asserts that a directory does not exist.
param string
$directoryparam string
$message
assertDirectoryExists
Asserts that a directory exists.
param string
$directoryparam string
$message
assertDirectoryIsNotReadable
Asserts that a directory exists and is not readable.
param string
$directoryparam string
$message
assertDirectoryIsNotWritable
Asserts that a directory exists and is not writable.
param string
$directoryparam string
$message
assertDirectoryIsReadable
Asserts that a directory exists and is readable.
param string
$directoryparam string
$message
assertDirectoryIsWritable
Asserts that a directory exists and is writable.
param string
$directoryparam string
$message
assertDoesNotMatchRegularExpression
Asserts that a string does not match a given regular expression.
param string
$patternparam string
$stringparam string
$message
assertEmpty
Asserts that a variable is empty.
param
$actualparam string
$message
assertEquals
Asserts that two variables are equal.
param
$expectedparam
$actualparam string
$message
assertEqualsCanonicalizing
Asserts that two variables are equal (canonicalizing).
param
$expectedparam
$actualparam string
$message
assertEqualsIgnoringCase
Asserts that two variables are equal (ignoring case).
param
$expectedparam
$actualparam string
$message
assertEqualsWithDelta
Asserts that two variables are equal (with delta).
param
$expectedparam
$actualparam float
$deltaparam string
$message
assertFalse
Asserts that a condition is false.
param
$conditionparam string
$message
assertFileDoesNotExist
Asserts that a file does not exist.
param string
$filenameparam string
$message
assertFileEquals
Asserts that the contents of one file is equal to the contents of another file.
param string
$expectedparam string
$actualparam string
$message
assertFileEqualsCanonicalizing
Asserts that the contents of one file is equal to the contents of another file (canonicalizing).
param
$expectedparam
$actualparam string
$message
assertFileEqualsIgnoringCase
Asserts that the contents of one file is equal to the contents of another file (ignoring case).
param
$expectedparam
$actualparam string
$message
assertFileExists
Asserts that a file exists.
param string
$filenameparam string
$message
assertFileIsNotReadable
Asserts that a file exists and is not readable.
param string
$fileparam string
$message
assertFileIsNotWritable
Asserts that a file exists and is not writable.
param string
$fileparam string
$message
assertFileIsReadable
Asserts that a file exists and is readable.
param string
$fileparam string
$message
assertFileIsWritable
Asserts that a file exists and is writable.
param string
$fileparam string
$message
assertFileNotEquals
Asserts that the contents of one file is not equal to the contents of another file.
param
$expectedparam
$actualparam string
$message
assertFileNotEqualsCanonicalizing
Asserts that the contents of one file is not equal to the contents of another file (canonicalizing).
param
$expectedparam
$actualparam string
$message
assertFileNotEqualsIgnoringCase
Asserts that the contents of one file is not equal to the contents of another file (ignoring case).
param
$expectedparam
$actualparam string
$message
assertFileNotExists
Asserts that a file does not exist.
param string
$filenameparam string
$message
assertFinite
Asserts that a variable is finite.
param
$actualparam string
$message
assertGreaterOrEquals
Asserts that a value is greater than or equal to another value.
param
$expectedparam
$actualparam string
$message
assertGreaterThan
Asserts that a value is greater than another value.
param
$expectedparam
$actualparam string
$message
assertGreaterThanOrEqual
Asserts that a value is greater than or equal to another value.
param
$expectedparam
$actualparam string
$message
assertInfinite
Asserts that a variable is infinite.
param
$actualparam string
$message
assertInstanceOf
Asserts that a variable is of a given type.
param
$expectedparam
$actualparam string
$message
assertIsArray
Asserts that a variable is of type array.
param
$actualparam string
$message
assertIsBool
Asserts that a variable is of type bool.
param
$actualparam string
$message
assertIsCallable
Asserts that a variable is of type callable.
param
$actualparam string
$message
assertIsClosedResource
Asserts that a variable is of type resource and is closed.
param
$actualparam string
$message
assertIsEmpty
Asserts that a variable is empty.
param
$actualparam string
$message
assertIsFloat
Asserts that a variable is of type float.
param
$actualparam string
$message
assertIsInt
Asserts that a variable is of type int.
param
$actualparam string
$message
assertIsIterable
Asserts that a variable is of type iterable.
param
$actualparam string
$message
assertIsNotArray
Asserts that a variable is not of type array.
param
$actualparam string
$message
assertIsNotBool
Asserts that a variable is not of type bool.
param
$actualparam string
$message
assertIsNotCallable
Asserts that a variable is not of type callable.
param
$actualparam string
$message
assertIsNotClosedResource
Asserts that a variable is not of type resource.
param
$actualparam string
$message
assertIsNotFloat
Asserts that a variable is not of type float.
param
$actualparam string
$message
assertIsNotInt
Asserts that a variable is not of type int.
param
$actualparam string
$message
assertIsNotIterable
Asserts that a variable is not of type iterable.
param
$actualparam string
$message
assertIsNotNumeric
Asserts that a variable is not of type numeric.
param
$actualparam string
$message
assertIsNotObject
Asserts that a variable is not of type object.
param
$actualparam string
$message
assertIsNotReadable
Asserts that a file/dir exists and is not readable.
param string
$filenameparam string
$message
assertIsNotResource
Asserts that a variable is not of type resource.
param
$actualparam string
$message
assertIsNotScalar
Asserts that a variable is not of type scalar.
param
$actualparam string
$message
assertIsNotString
Asserts that a variable is not of type string.
param
$actualparam string
$message
assertIsNotWritable
Asserts that a file/dir exists and is not writable.
param
$filenameparam string
$message
assertIsNumeric
Asserts that a variable is of type numeric.
param
$actualparam string
$message
assertIsObject
Asserts that a variable is of type object.
param
$actualparam string
$message
assertIsReadable
Asserts that a file/dir is readable.
param
$filenameparam string
$message
assertIsResource
Asserts that a variable is of type resource.
param
$actualparam string
$message
assertIsScalar
Asserts that a variable is of type scalar.
param
$actualparam string
$message
assertIsString
Asserts that a variable is of type string.
param
$actualparam string
$message
assertIsWritable
Asserts that a file/dir exists and is writable.
param
$filenameparam string
$message
assertJson
Asserts that a string is a valid JSON string.
param string
$actualJsonparam string
$message
assertJsonFileEqualsJsonFile
Asserts that two JSON files are equal.
param string
$expectedFileparam string
$actualFileparam string
$message
assertJsonFileNotEqualsJsonFile
Asserts that two JSON files are not equal.
param string
$expectedFileparam string
$actualFileparam string
$message
assertJsonStringEqualsJsonFile
Asserts that the generated JSON encoded object and the content of the given file are equal.
param string
$expectedFileparam string
$actualJsonparam string
$message
assertJsonStringEqualsJsonString
Asserts that two given JSON encoded objects or arrays are equal.
param string
$expectedJsonparam string
$actualJsonparam string
$message
assertJsonStringNotEqualsJsonFile
Asserts that the generated JSON encoded object and the content of the given file are not equal.
param string
$expectedFileparam string
$actualJsonparam string
$message
assertJsonStringNotEqualsJsonString
Asserts that two given JSON encoded objects or arrays are not equal.
param string
$expectedJsonparam string
$actualJsonparam string
$message
assertLessOrEquals
Asserts that a value is smaller than or equal to another value.
param
$expectedparam
$actualparam string
$message
assertLessThan
Asserts that a value is smaller than another value.
param
$expectedparam
$actualparam string
$message
assertLessThanOrEqual
Asserts that a value is smaller than or equal to another value.
param
$expectedparam
$actualparam string
$message
assertMatchesRegularExpression
Asserts that a string matches a given regular expression.
param string
$patternparam string
$stringparam string
$message
assertNan
Asserts that a variable is nan.
param
$actualparam string
$message
assertNotContains
Asserts that a haystack does not contain a needle.
param
$needleparam
$haystackparam string
$message
assertNotContainsEquals
not documented
assertNotContainsOnly
Asserts that a haystack does not contain only values of a given type.
param string
$typeparam
$haystackparam bool|null
$isNativeTypeparam string
$message
assertNotCount
Asserts the number of elements of an array, Countable or Traversable.
param int
$expectedCountparam Countable|iterable
$haystackparam string
$message
assertNotEmpty
Asserts that a variable is not empty.
param
$actualparam string
$message
assertNotEquals
Asserts that two variables are not equal.
param
$expectedparam
$actualparam string
$message
assertNotEqualsCanonicalizing
Asserts that two variables are not equal (canonicalizing).
param
$expectedparam
$actualparam string
$message
assertNotEqualsIgnoringCase
Asserts that two variables are not equal (ignoring case).
param
$expectedparam
$actualparam string
$message
assertNotEqualsWithDelta
Asserts that two variables are not equal (with delta).
param
$expectedparam
$actualparam float
$deltaparam string
$message
assertNotFalse
Asserts that a condition is not false.
param
$conditionparam string
$message
assertNotInstanceOf
Asserts that a variable is not of a given type.
param
$expectedparam
$actualparam string
$message
assertNotNull
Asserts that a variable is not null.
param
$actualparam string
$message
assertNotRegExp
Asserts that a string does not match a given regular expression.
param string
$patternparam string
$stringparam string
$message
assertNotSame
Asserts that two variables do not have the same type and value.
param
$expectedparam
$actualparam string
$message
assertNotSameSize
Assert that the size of two arrays (or Countable
or Traversable
objects) is not the same.
param Countable|iterable
$expectedparam Countable|iterable
$actualparam string
$message
assertNotTrue
Asserts that a condition is not true.
param
$conditionparam string
$message
assertNull
Asserts that a variable is null.
param
$actualparam string
$message
assertObjectHasAttribute
Asserts that an object has a specified attribute.
param string
$attributeNameparam object
$objectparam string
$message
assertObjectNotHasAttribute
Asserts that an object does not have a specified attribute.
param string
$attributeNameparam object
$objectparam string
$message
assertRegExp
Asserts that a string matches a given regular expression.
param string
$patternparam string
$stringparam string
$message
assertSame
Asserts that two variables have the same type and value.
param
$expectedparam
$actualparam string
$message
assertSameSize
Assert that the size of two arrays (or Countable
or Traversable
objects) is the same.
param Countable|iterable
$expectedparam Countable|iterable
$actualparam string
$message
assertStringContainsString
param string
$needleparam string
$haystackparam string
$message
assertStringContainsStringIgnoringCase
not documented
assertStringEndsNotWith
Asserts that a string ends not with a given suffix.
param string
$suffixparam string
$stringparam string
$message
assertStringEndsWith
Asserts that a string ends with a given suffix.
param string
$suffixparam string
$stringparam string
$message
assertStringEqualsFile
Asserts that the contents of a string is equal to the contents of a file.
param string
$expectedFileparam string
$actualStringparam string
$message
assertStringEqualsFileCanonicalizing
Asserts that the contents of a string is equal to the contents of a file (canonicalizing).
param string
$expectedFileparam string
$actualStringparam string
$message
assertStringEqualsFileIgnoringCase
Asserts that the contents of a string is equal to the contents of a file (ignoring case).
param string
$expectedFileparam string
$actualStringparam string
$message
assertStringMatchesFormat
Asserts that a string matches a given format string.
param string
$formatparam string
$stringparam string
$message
assertStringMatchesFormatFile
Asserts that a string matches a given format file.
param string
$formatFileparam string
$stringparam string
$message
assertStringNotContainsString
param string
$needleparam string
$haystackparam string
$message
assertStringNotContainsStringIgnoringCase
param string
$needleparam string
$haystackparam string
$message
assertStringNotEqualsFile
Asserts that the contents of a string is not equal to the contents of a file.
param string
$expectedFileparam string
$actualStringparam string
$message
assertStringNotEqualsFileCanonicalizing
Asserts that the contents of a string is not equal to the contents of a file (canonicalizing).
param string
$expectedFileparam string
$actualStringparam string
$message
assertStringNotEqualsFileIgnoringCase
Asserts that the contents of a string is not equal to the contents of a file (ignoring case).
param string
$expectedFileparam string
$actualStringparam string
$message
assertStringNotMatchesFormat
Asserts that a string does not match a given format string.
param string
$formatparam string
$stringparam string
$message
assertStringNotMatchesFormatFile
Asserts that a string does not match a given format string.
param string
$formatFileparam string
$stringparam string
$message
assertStringStartsNotWith
Asserts that a string starts not with a given prefix.
param string
$prefixparam string
$stringparam string
$message
assertStringStartsWith
Asserts that a string starts with a given prefix.
param string
$prefixparam string
$stringparam string
$message
assertThat
Evaluates a PHPUnit\Framework\Constraint matcher object.
param
$valueparam Constraint
$constraintparam string
$message
assertThatItsNot
Evaluates a PHPUnit\Framework\Constraint matcher object.
param
$valueparam Constraint
$constraintparam string
$message
assertTrue
Asserts that a condition is true.
param
$conditionparam string
$message
assertXmlFileEqualsXmlFile
Asserts that two XML files are equal.
param string
$expectedFileparam string
$actualFileparam string
$message
assertXmlFileNotEqualsXmlFile
Asserts that two XML files are not equal.
param string
$expectedFileparam string
$actualFileparam string
$message
assertXmlStringEqualsXmlFile
Asserts that two XML documents are equal.
param string
$expectedFileparam DOMDocument|string
$actualXmlparam string
$message
assertXmlStringEqualsXmlString
Asserts that two XML documents are equal.
param DOMDocument|string
$expectedXmlparam DOMDocument|string
$actualXmlparam string
$message
assertXmlStringNotEqualsXmlFile
Asserts that two XML documents are not equal.
param string
$expectedFileparam DOMDocument|string
$actualXmlparam string
$message
assertXmlStringNotEqualsXmlString
Asserts that two XML documents are not equal.
param DOMDocument|string
$expectedXmlparam DOMDocument|string
$actualXmlparam string
$message
expectException
Handles and checks exception called inside callback function. Either exception class name or exception instance should be provided.
<?php
$I->expectException(MyException::class, function() {
$this->doSomethingBad();
});
$I->expectException(new MyException(), function() {
$this->doSomethingBad();
});
If you want to check message or exception code, you can pass them with exception instance:
<?php
// will check that exception MyException is thrown with "Don't do bad things" message
$I->expectException(new MyException("Don't do bad things"), function() {
$this->doSomethingBad();
});
@deprecated Use expectThrowable() instead
param \Exception|string
$exceptionparam callable
$callback
expectThrowable
Handles and checks throwables (Exceptions/Errors) called inside the callback function. Either throwable class name or throwable instance should be provided.
<?php
$I->expectThrowable(MyThrowable::class, function() {
$this->doSomethingBad();
});
$I->expectThrowable(new MyException(), function() {
$this->doSomethingBad();
});
If you want to check message or throwable code, you can pass them with throwable instance:
<?php
// will check that throwable MyError is thrown with "Don't do bad things" message
$I->expectThrowable(new MyError("Don't do bad things"), function() {
$this->doSomethingBad();
});
param \Throwable|string
$throwableparam callable
$callback
fail
Fails a test with the given message.
param string
$message
markTestIncomplete
Mark the test as incomplete.
param string
$message
markTestSkipped
Mark the test as skipped.
param string
$message
© 2011 Michael Bodnarchuk and contributors
Licensed under the MIT License.
https://codeception.com/docs/modules/Asserts