GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing codeception.com — Modules And Helpers - Codeception Docs Guides
Testomatio - Test Management for Codeception Search💡 You are reading docs for latest Codeception 5. Read for 4.x
Modules and HelpersCodeception uses modularity to create a comfortable testing environment for every test suite you write. All actions and assertions that can be performed by the Tester object in a class are defined in modules. You can extend the testing suite with your own actions and assertions by writing them into a custom module. Let’s look at the following test: It can operate with different entities: the web page can be loaded with the PhpBrowser module, the database assertion uses the Db module, and file state can be checked with the Filesystem module. Modules are attached to the Actor classes in the suite configuration.
For example, in The FunctionalTester class has its methods defined in modules. Actually, it doesn’t contain any of them,
but rather acts as a proxy. It knows which module executes this action and passes parameters into it.
To make your IDE see all of the FunctionalTester methods, you should run the Standard ModulesCodeception has many bundled modules which will help you run tests for different purposes and different environments. The idea of modules is to share common actions, so that developers and QA engineers can concentrate on testing and not on reinventing the wheel. Each module provides methods for testing its own part and by combining modules you can get a powerful setup to test an application at all levels. There is the Module ConflictsModules may conflict with one another. If a module implements This way if you try to use two modules sharing the same conflicted interface you will get an exception. To avoid confusion, Framework modules, PhpBrowser, and WebDriver can’t be used together. For instance,
the In case you need to use a module which depends on a conflicted one, specify it as a dependent module in the configuration.
You may want to use This configuration will allow you to send GET/POST requests to the server’s APIs while working with a site through a browser. If you only need some parts of a conflicted module to be loaded, refer to the next section. Module PartsModules with Parts section in their reference can be partially loaded. This way, the For instance, the Laravel module has an ORM part which contains database actions. You can enable the PhpBrowser module for testing and Laravel’s ORM part for connecting to the database and checking the data. The modules won’t conflict as actions with the same names won’t be loaded. The REST module has parts for HelpersCodeception doesn’t restrict you to only the modules from the main repository.
Your project might need your own actions added to the test suite. By running the Actions are also quite simple. Every action you define is a public function. Write a public method,
then run the
Public methods prefixed by `_` are treated as hidden and won't be added to your Actor class.
Assertions can be a bit tricky. First of all, it’s recommended to prefix all your assertion actions with Name your assertions like this: And then use them in your tests: You can define assertions by using assertXXX methods in your modules. In your helpers you can use these assertions: Accessing Other ModulesIt’s possible that you will need to access internal data or functions from other modules. For example, for your module you might need to access the responses or internal actions of other modules. Modules can interact with each other through the Let’s imagine that we are writing a module that reconnects to a database. It’s supposed to use the dbh connection value from the Db module. By using the Modules may also contain methods that are exposed for use in helper classes. Those methods start with a You should use them to write your own actions using module internals. In this example we use the API of the facebook/php-webdriver library,
a Selenium WebDriver client the module is build on.
You can also access the Extending a ModuleIf accessing modules doesn’t provide enough flexibility, you can extend a module inside a Helper class: In this helper you can replace the parent’s methods with your own implementation.
You can also replace the HooksEach module can handle events from the running test. A module can be executed before the test starts,
or after the test is finished. This can be useful for bootstrap/cleanup actions.
You can also define special behavior for when the test fails. This may help you in debugging the issue.
For example, the PhpBrowser module saves the current webpage to the All hooks are defined in Codeception\Module and are listed here. You are free to redefine them in your module. Please note that methods with a DebugAs we mentioned, the Each module can output internal values that may be useful during debug. For example, the PhpBrowser module prints the response code and current URL every time it moves to a new page. Thus, modules are not black boxes. They are trying to show you what is happening during the test. This makes debugging your tests less painful. To display additional information, use the This test, running with the PhpBrowser module in debug mode, will print something like this: ConfigurationModules and Helpers can be configured from the suite configuration file, or globally from Mandatory parameters should be defined in the The next time you start the suite without setting one of these values, an exception will be thrown. For optional parameters, you should set default values. The The host and port parameter can be redefined in the suite configuration.
Values are set in the Optional and mandatory parameters can be accessed through the Dynamic Configuration With ParametersModules can be dynamically configured from environment variables.
Parameter storage should be specified in the global Use the Example: load parameters from the environment: Example: load parameters from YAML file (Symfony): Example: load parameters from php file (Yii) Example: load parameters from .Env files (Laravel): Once loaded, parameter variables can be used as module configuration values.
Use a variable name wrapped with Let’s say we want to specify credentials for a cloud testing service. We have loaded Parameters are also useful to provide connection credentials for the Parameters can set in JSON format. So objects, arrays, booleans and strings can also be passed into config. In this case Runtime ConfigurationIf you want to reconfigure a module at runtime, you need to call a helper that uses the In this example we change the root URL for PhpBrowser, so that Usually, these configuration changes are effective immediately. However, in WebDriver configuration changes can’t be applied that easily.
For instance, if you change the browser you need to close the current browser session and start a new one.
For that, WebDriver module provides a
At the end of a test all configuration changes will be rolled back to the original configuration values. Runtime Configuration of a TestSometimes it is needed to set custom configuration for a specific test only.
For Cest and Test\Unit
formats you can use To run a specific test only in Chrome browser, you can call Prepare methods can invoke all methods of a module, as well as hidden API methods (starting with To change module configuration for a specific group of tests use GroupObjects. ConclusionModules are the real power of Codeception. They are used to emulate multiple inheritances for Actor classes (UnitTester, FunctionalTester, AcceptanceTester, etc). Codeception provides modules to emulate web requests, access data, interact with popular PHP libraries, etc. If the bundled modules are not enough for you that’s OK, you are free to write your own! Use Helpers (custom modules) for everything that Codeception can’t do out of the box. Helpers also can be used to extend the functionality of the original modules.
Don't know how to write tests on your own? We will build or improve them for you.
Request a quote for official
enterprise
support or trainings
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |