Convert Simpletest test suite to PHPUnit - phpunit

I use Simpletest test suites to run all tests, with different configuration methods. I've created one suite per config method, and it sets up the environment and then runs all tests.
Take a look at Adapter.php and Constants.php if that's unclear.
Now there seems to be support for test suites in PHPUnit, but as I understand it, it's merely for grouping tests with no support for setting up the environment or running PHP scripts.
How can I convert my test suites to PHPUnit? I'm open to rethink how the tests are structured :)

You can use test fixtures to setup your environment either before each test, or before each test suite:
http://www.phpunit.de/manual/3.6/en/fixtures.html#fixtures.more-setup-than-teardown
The methods you are looking for are
setUp() and tearDown() (called before/after each single test), and
setUpBeforeClass() and tearDownAfterClass() (called before/after each test class (suite)).

Apart from restructuring your test suite you should also be aware that the behaviour of the assertions in PHPUnit is different from SimpleTest.
While in SimpleTest you can test on an assertions result and execute further code after a failed assertion, you cannot in PHPUnit: test the return value of a method that triggers an error with PHPUnit
This will likely also force refactoring some of the actual tests.

Related

Jest Integration Testing - How to manage global context?

It doesn't seem like Jest allows all the test runs to share a global context for integration testing. It does have globalSetup and globalTeardown options, but it doesn't seem like those hooks are intended to be a shared testing context across all test runs. You can hack something together using these hooks, passing variables through the node process, but then other issues start to arise (Different instances of Array constructors during assertions, etc...).
If I have a fairly heavy integration test setup process, is it recommended that I only have one test run, where all my integration tests would live and have access to the shared context?
Currently my integration tests are spread across multiple files (individual entry points) and import a setup process. However that setup process is getting run once per file. I'm assuming that alternatively I can have one entry point and then import all my integration test files within the same context. Is that a good/recommended option?
What's the recommended way of setting up integration tests for use in Jest?
I've solved this issue using a combination of 2 solutions:
Jest setupTestFrameworkScriptFile: https://jestjs.io/docs/en/23.x/configuration#setuptestframeworkscriptfile-string
NodeJS Global objects: https://stackabuse.com/using-global-variables-in-node-js/
Using the jest one time setup I can execute my setup only once, and store the results as variables in the NodeJS global object.
Example:
// package.json
"jest": {
"setupTestFrameworkScriptFile": "<rootDir>/globalSetupFile.js"
}
// globalSetupFile.js
global.globalUser = createGlobalUser();
// testFile.spec.js
loginAsUser(global.globalUser.email, global.globalUser.password);

Spring Cloud Contract with TestNG

As far as I understand(beginner level on Spring Cloud Contract), the contract tests generated from the groovy contracts are with junit.
What if I want to use TestNG? Basically I want to group my tests with TestNG annotation #Test(groups="unit") and #Test(groups="contract") in order to separate unit tests and contract tests using ssomething like mvn test -Dgroups=unit and mvn test -Dgroups=contract.
Thank you for your help.
Currently, we don't support this out of the box. You would have to write your own org.springframework.cloud.contract.verifier.builder.SingleTestGenerator implementation (by extending, for example, the JavaTestGenerator and referencing it in spring.factories file) but that can be quite time-consuming, unfortunately.

Accessing Karma Configurations from a Jasmine Unit Test

I was wondering if it was possible to access the settings defined within my karma.config.js file, and if so how?
I'm currently using a Grunt task runner to perform various tasks like building, linting, packaging, etc. I'm also using Grunt to kick off the Karma test runner to run my Jasmine unit tests. Furthermore, I'm pulling in the Jasmine-jQuery library so I can define and read in JSON and HTML fixtures from separate files that I setup earlier.
While I was writing some new tests, I noticed that I was redefining my fixtures base path in every test file. So I decided to pull it out and put it into a global constant.
The problem I'm having is where I should define it. Currently, I have a file named "testSettings.js" that I'm including in my karma configuration, where I define a configuration object and set it to window.testSettings. Thats all well and good, but I think it would be better if I defined it within my karma configuration and then just referenced that from my tests. But there doesn't look like a way to do this... or is there?
My library versions are:
"karma": "~0.12.32"
"karma-jasmine": "~0.3.5"
"karma-jasmine-jquery": "~0.1.1"

Execute specific test scenarios in a suite using Web driver

I have a suite with more than 500 test scenarios written in Web driver. I am using TestNG framework to execute the test cases.
Could you please suggest me how to execute specific test cases from the suite like 31, 45, 68 etc?
Thank you
If you have prefaced all of your tests with #Test, then in Eclipse with TestNG plugin, you can right click on the method name, and run it as a TestNG Test. You can also click on a class file, and only run that class. However, if you want to run test X and test Y that are in different tests, you can press Run Configurations... and add which methods you want run.
If you don't have the TestNG plugin, you can write an XML file that has the classes, methods, groups, or suites you want to run. It is described here

Test suites info in PHPUnit bootstrap file

I am running test using a phpunit.xml.dist file. This file defines several test suites and specifies a bootstrap.php. In this bootstrap.php I am currently loading all dependencies for all tests.
A small subset of the tests is dependent on some third party library, which is optional. These tests are all part of a particular test suite. So I only want to load this library in the bootstrapping file when that particular test suite is specified.
How can I determine if this test suite was specified? This then ensures that most tests can be run when the library is not loaded, and that one can easily verify the code and tests that should not depend on the library indeed do not need it.
I currently have the following. Is there something better?
if ( !in_array( '--testsuite=WikibaseDatabaseStandalone', $GLOBALS['argv'] ) ) {
require_once( __DIR__ . '/evilMediaWikiBootstrap.php' );
}
The feature request on the PHPUnit bugtracker for a test suite specific bootstrap is here: https://github.com/sebastianbergmann/phpunit/issues/733
For now there are two options: One is yours which is fine but feels really hackish and doesn't work out well if you run "all the tests" if you have specific bootstrap for every one of them.
My suggestion would be to write a test listener and hook into "startTestSuite" and "endTestSuite". This is a nice maintained and BC compatible way to execute code only when the test suite is actually started and you can also clean up afterwards.
See http://phpunit.de/manual/3.7/en/extending-phpunit.html#extending-phpunit.PHPUnit_Framework_TestListener and http://phpunit.de/manual/3.7/en/appendixes.configuration.html#appendixes.configuration.test-listeners for how to include the test listener.
One of the usual way to handle this is to check if a required dependency is installed, and if not, run
$this->markTestAsSkipped('lib not installed');
That skipping can also happen in the setUp() phase of a test.
Finally, you can add #group annotations to the test-class and/or test functions to give some choice to whether or not the test is run from the command line (with the --group [names...] parameter).
Finally, an option that has also been used in the ZendFramework is to only add the TestSuite that runs a subset within a larger set of a testsuite - in code. There is an example of being able to
a) turn off as will,
b) turn off if the extension is not loaded, or
c) run the tests, for the use of (for example)
caching with APC

Resources