SlideShare a Scribd company logo
AUTOMATED
TESTING IN DRUPAL
8
• @jaypan on Drupal.org and drupal.stackexchange.com
• Acquia certified Drupal Grandmaster
• Acquia Certified Developer
• Acquia Certified Developer – Front end specialist
• Acquia Certified Developer – Back end specialist
• Living in Japan since 2000
• Working with Drupal since 2007
• Currently writing a book on coding in Drupal
JAY FRIENDLY
JAY FRIENDLY
• Owner of Jaypan in Yokohama
• English https://www.jaypan.com
• Japanese https://www.jaypan.jp
• Jaypan specializes in
• high-performance AJAX/JavaScript heavy applications
• Custom module/theme development
• App Development
• Drupal integration with outside technologies
• JavaScript libraries
• Linux programs
• Multiple Drupal instances
• Technical consulting and translation (Japanese/English)
OVERVIEW
• Manual testing vs. automated testing
• How do automated tests work?
• History of testing in Drupal
• Types of automated tests in Drupal
• Discussion of each type of test
MANUAL TESTING
• Developer creates new functionality
• Developer tests new functionality
• Other people (client, team-members) test new functionality
• Found bugs are fixed
PROBLEMS WITH MANUAL TESTING
• Complex functionality requires significant time to test
• Easy to forget to test a given scenario
• Changes to code require re-testing every scenario after any change
• Changes to code in other parts of the system may break functionality
without anyone realizing it
• As time passes, the conditions that need to be tested may be
forgotten or lost as project members change
• Coming back to code after a period of time means the developer is
not sure what is required
AUTOMATED TESTING
• Developer creates automated tests that run through all testing
scenarios
• Testing system runs all tests and reports any behaviors that are
unexpected (aka bugs)
BENEFITS OF AUTOMATED TESTING
• Removes requirement of getting humans to test after each code
change
• Tests are faster than human testing
• Tests for the whole system can be run before committing any code to
a production server, to ensure new functionality in one part of the
system hasn’t broken functionality in another part of the system.
• If (or rather, when) new bugs are found, tests can be added along
with the fix, to ensure that the bug never arises again
• Overall system is more stable.
PROBLEMS WITH AUTOMATED TESTING
• Writing tests takes a significant amount of time
• Extra time means extra development resources
• Clients cannot directly see tests, and may not understand the extra costs
in developing when there is nothing for them to look at.
• Tests only test what they have been written to test. If the developer does
not think to test something, a bug could exist. The existence of automated
tests in such a case may create a false sense of security
• Tests happen in a virtual (non-visible) environment, making them hard to
debug (and even create)
• Changing code often requires re-writing old tests
OR IN OTHER WORDS, WHAT IS IT?
HOW DOES A TEST WORK?
• A test will first run some code, then do ‘assertions’ to ensure that the results work as
expected.
• For example, in Drupal 8, nodes have a method id(), that returns the ID of the
node.
• An automated test would create a new node object, then run $node->id() and test:
• Was a result returned?
• Is the result an integer?
• Is the returned ID the expected (correct) ID of the node that was created?
• Any failed assertions result in a failed test, telling the developer exactly what failed,
so they know what to fix.
AUTOMATED TESTING IN DRUPAL - HISTORY
• Drupal 6 - Simpletest module
• Drupal 7 - Simpletest in core
• Drupal 8 -
• Simpletest still in core but deprecated (to be removed in D9)
• PHPUnit introduced into core
• This presentation focuses exclusively on PHPUnit
TYPES OF TESTS IN DRUPAL 8
• Unit tests
• Kernel tests
• Functional tests
• JavaScript functional tests
UNIT TESTS IN DRUPAL 8
• A Unit is a PHP Class (OOP)
• Unit tests test code, not user-end functionality
• Unit tests will test a single class, to ensure that it behaves as
expected
• Unit tests test the unit (class) alone in isolation, rather than in the
Drupal environment
• Unit testing is very fast, as the system does not need to be
bootstrapped to run the tests
UNIT TEST OVERVIEW
• Unit tests go in [MODULENAME]/tests/src/Unit
• Namespace is DrupalTests[MODULENAME]Unit
• Unit tests extend DrupalTestsUnitTestCase
• Annotation should include (at a minimum)
• Class:
• @coversDefaultClass - the class the the test is testing
• @group - the group of tests (usually module name) that the test belongs to
• Method:
• @covers - the method of the class being tested
UNIT TEST OVERVIEW
• Testing class name should end in ‘Test’
• Example: the test for the Person class would be PersonTest
• Testing method (function) name must start with ‘test’
• Example: the method used to test the id() method of Person
would be testId()
UNIT TEST OVERVIEW
• Test classes can have a setUp() method, that sets up
conditions/objects before each test in the test class.
• Useful when all tests require a similar set of conditions
• Test classes can also have a tearDown() method, where objects
are cleaned up after each test in the test class.
• Test classes can have a setUpBeforeClass() method that is run
once before the test class is run
• Test classes can have a tearDownAfterClass() method that is run
once after the test class is finished
CLASS (UNIT) TO BE TESTED
class Person {
private $id;
public function setId($id) {
$this->id = $id;
}
public function id() {
return $this->id();
}
}
EXAMPLE UNIT TEST FOR PERSON CLASS
/**
* @coversDefaultClass Person
*/
class PersonTest {
/**
* @covers ::setId()
*/
public testId() {
$person = new Person();
$person->setId(123);
$this->assertNotNull($person->getId(), ‘A value is returned for the ID’);
$this->assertTrue(is_int($person->id()), ‘The returned ID is an
integer’);
$this->assertEqual(123, $this->getId(), ‘The returned ID is correct’);
}
}
KERNEL TESTS
KERNEL TESTS IN DRUPAL 8
• Kernel tests are executed in a limited Drupal environment, similar to
the environment before Drupal has been installed
• Tests can access files and the database
• Declared modules are loaded, but not installed. Installation needs
to be done manually
• Kernel tests are slower than Unit tests, but faster than Functional
tests
KERNEL TEST OVERVIEW
• Kernel tests go in [MODULENAME]/tests/src/Kernel
• Namespace is DrupalTests[MODULENAME]Kernel
• Kernel tests extend DrupalTestsKernelTestBast
EXAMPLE KERNEL TEST
Sorry no example!
FUNCTIONAL TESTS IN DRUPAL 8
• Functional tests work in an installed Drupal environment
• Modules needed for the test need to be declared for installation
• Tests use an internal browser (aka code - not visual), where links
can be clicked, form elements can be filled out, and pages can be
requested, all programmatically
• Users, roles, blocks and other entities can be created
• Tests are slow as each test builds a new Drupal instance in which
to run the tests
FUNCTIONAL TEST OVERVIEW
• Unit tests go in [MODULENAME]/tests/src/Functional
• Namespace is DrupalTests[MODULENAME]Functional
• Unit tests extend DrupalTestsBrowserTestBase
• Annotation should include (at a minimum)
• Class:
• @group - the group of tests (usually module name) that the test belongs to
• $modules property contains an array of modules to be installed
• example: $modules = [‘node’, ‘restrict_ip’]
FROM RESTRICT IP MODULE
EXAMPLE FUNCTIONAL TEST
namespace DrupalTestsrestrict_ipFunctional;
/**
* @group restrict_ip
*/
class RestrictIpAccessTest extends RestrictIpBrowserTestBase // which extends BrowserTestBase
{
protected static $modules = ['restrict_ip', 'node'];
/**
* Test that a user is blocked when the module is enabled
*/
public function testModuleEnabled()
{
$adminUser = $this->drupalCreateUser(['administer restricted ip addresses', 'access administration
pages', 'administer modules']);
$this->drupalLogin($adminUser);
$this->drupalGet('admin/config/people/restrict_ip');
$this->assertStatusCodeEquals(200);
$this->checkCheckbox('#edit-enable');
$this->click('#edit-submit');
$this->assertSession()->pageTextContains('The page you are trying to access cannot be accessed from
your IP address.');
}
}
JAVASCRIPT FUNCTIONAL TESTS IN DRUPAL
8
• JavaScript Functional tests work in a fully installed Drupal
environment
• Extends Functional tests to include JavaScript
• Require PhantomJS browser
• Virtual browser (no GUI)
• Requires installation on testing computer/server
• Can test #ajax methods, as well as other JavaScript functionality
JAVASCRIPT FUNCTIONAL TEST OVERVIEW
• Unit tests go in [MODULENAME]/tests/src/FunctionalJavascript
• Namespace is
DrupalTests[MODULENAME]FunctionalJavascript
• Unit tests extend
DrupalFunctionalJavascriptTestsJavascriptTestBase
• Annotation should include (at a minimum)
• Class:
• @group - the group of tests (usually module name) that the test
belongs to
FROM FANCY LOGIN MODULE
EXAMPLE JAVASCRIPT FUNCTIONAL TEST
namespace DrupalTestsfancy_loginFunctionalJavascript;
/**
* @group fancy_login
*/
class FancyLoginJavascriptTest extends FancyLoginJavascriptTestBase // which extends JavascriptTestBase
{
public function testDimmerBackgroundColor()
{
$this->openPopup();
$this->assertJsCondition('jQuery("#fancy_login_dim_screen").not(":animated")');
$this->assertJsCondition('jQuery("#fancy-login-user-login-form").not(":animated")');
$script = $this->createScriptString('#fancy_login_dim_screen', 'background-color', 'rgb(255, 255,
255)');
if(!$this->getSession()->evaluateScript($script))
{
throw new Exception('background-color is not white');
}
// …
}
}
WRITE TESTS THEN CODE
TESTING DRIVEN DEVELOPMENT
• Write tests as the start of the project, rather than the end
• Developers build and test as they go
• Gives a clear indicator of what to do next
• Gives a clear indication of when the project is complete
• Note - testing driven development is purely theoretical for me - I’ve
never done it
WHERE TO GO FROM HERE
• PHPUnit in Drupal 8 - https://www.drupal.org/docs/8/phpunit
• Documentation is limited
• Lots and lots of Googling
Thank you!
(Questions?)

More Related Content

What's hot

3 WAYS TO TEST YOUR COLDFUSION API
3 WAYS TO TEST YOUR COLDFUSION API3 WAYS TO TEST YOUR COLDFUSION API
3 WAYS TO TEST YOUR COLDFUSION API
Gavin Pickin
 
Painless JavaScript Testing with Jest
Painless JavaScript Testing with JestPainless JavaScript Testing with Jest
Painless JavaScript Testing with Jest
Michał Pierzchała
 
Xdebug and Drupal8 tests (PhpUnit and Simpletest)
Xdebug and Drupal8 tests (PhpUnit and Simpletest)Xdebug and Drupal8 tests (PhpUnit and Simpletest)
Xdebug and Drupal8 tests (PhpUnit and Simpletest)
Francisco José Seva Mora
 
How to test models using php unit testing framework?
How to test models using php unit testing framework?How to test models using php unit testing framework?
How to test models using php unit testing framework?
satejsahu
 
Testing Ext JS and Sencha Touch
Testing Ext JS and Sencha TouchTesting Ext JS and Sencha Touch
Testing Ext JS and Sencha Touch
Mats Bryntse
 
Unit-testing and E2E testing in JS
Unit-testing and E2E testing in JSUnit-testing and E2E testing in JS
Unit-testing and E2E testing in JS
Michael Haberman
 
Unit testing presentation
Unit testing presentationUnit testing presentation
Unit testing presentation
Arthur Freyman
 
Testing in AngularJS
Testing in AngularJSTesting in AngularJS
Testing in AngularJS
Peter Drinnan
 
Unit testing - A&BP CC
Unit testing - A&BP CCUnit testing - A&BP CC
Unit testing - A&BP CC
JWORKS powered by Ordina
 
CI / CD w/ Codeception
CI / CD w/ CodeceptionCI / CD w/ Codeception
CI / CD w/ Codeception
Tudor Barbu
 
Workshop: Functional testing made easy with PHPUnit & Selenium (phpCE Poland,...
Workshop: Functional testing made easy with PHPUnit & Selenium (phpCE Poland,...Workshop: Functional testing made easy with PHPUnit & Selenium (phpCE Poland,...
Workshop: Functional testing made easy with PHPUnit & Selenium (phpCE Poland,...
Ondřej Machulda
 
Jest: Frontend Testing leicht gemacht @EnterJS2018
Jest: Frontend Testing leicht gemacht @EnterJS2018Jest: Frontend Testing leicht gemacht @EnterJS2018
Jest: Frontend Testing leicht gemacht @EnterJS2018
Holger Grosse-Plankermann
 
Scala for Test Automation
Scala for Test AutomationScala for Test Automation
Scala for Test Automation
rthanavarapu
 
Token Testing Slides
Token  Testing SlidesToken  Testing Slides
Token Testing Slides
ericholscher
 
JavaFX8 TestFX - CDI
JavaFX8   TestFX - CDIJavaFX8   TestFX - CDI
JavaFX8 TestFX - CDI
Sven Ruppert
 
Integration Testing With ScalaTest and MongoDB
Integration Testing With ScalaTest and MongoDBIntegration Testing With ScalaTest and MongoDB
Integration Testing With ScalaTest and MongoDB
Michal Bigos
 
Developers Testing - Girl Code at bloomon
Developers Testing - Girl Code at bloomonDevelopers Testing - Girl Code at bloomon
Developers Testing - Girl Code at bloomon
Ineke Scheffers
 
Unit testing JavaScript: Jasmine & karma intro
Unit testing JavaScript: Jasmine & karma introUnit testing JavaScript: Jasmine & karma intro
Unit testing JavaScript: Jasmine & karma intro
Maurice De Beijer [MVP]
 
Testing Legacy Rails Apps
Testing Legacy Rails AppsTesting Legacy Rails Apps
Testing Legacy Rails Apps
Rabble .
 
JavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and KarmaJavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and Karma
Christopher Bartling
 

What's hot (20)

3 WAYS TO TEST YOUR COLDFUSION API
3 WAYS TO TEST YOUR COLDFUSION API3 WAYS TO TEST YOUR COLDFUSION API
3 WAYS TO TEST YOUR COLDFUSION API
 
Painless JavaScript Testing with Jest
Painless JavaScript Testing with JestPainless JavaScript Testing with Jest
Painless JavaScript Testing with Jest
 
Xdebug and Drupal8 tests (PhpUnit and Simpletest)
Xdebug and Drupal8 tests (PhpUnit and Simpletest)Xdebug and Drupal8 tests (PhpUnit and Simpletest)
Xdebug and Drupal8 tests (PhpUnit and Simpletest)
 
How to test models using php unit testing framework?
How to test models using php unit testing framework?How to test models using php unit testing framework?
How to test models using php unit testing framework?
 
Testing Ext JS and Sencha Touch
Testing Ext JS and Sencha TouchTesting Ext JS and Sencha Touch
Testing Ext JS and Sencha Touch
 
Unit-testing and E2E testing in JS
Unit-testing and E2E testing in JSUnit-testing and E2E testing in JS
Unit-testing and E2E testing in JS
 
Unit testing presentation
Unit testing presentationUnit testing presentation
Unit testing presentation
 
Testing in AngularJS
Testing in AngularJSTesting in AngularJS
Testing in AngularJS
 
Unit testing - A&BP CC
Unit testing - A&BP CCUnit testing - A&BP CC
Unit testing - A&BP CC
 
CI / CD w/ Codeception
CI / CD w/ CodeceptionCI / CD w/ Codeception
CI / CD w/ Codeception
 
Workshop: Functional testing made easy with PHPUnit & Selenium (phpCE Poland,...
Workshop: Functional testing made easy with PHPUnit & Selenium (phpCE Poland,...Workshop: Functional testing made easy with PHPUnit & Selenium (phpCE Poland,...
Workshop: Functional testing made easy with PHPUnit & Selenium (phpCE Poland,...
 
Jest: Frontend Testing leicht gemacht @EnterJS2018
Jest: Frontend Testing leicht gemacht @EnterJS2018Jest: Frontend Testing leicht gemacht @EnterJS2018
Jest: Frontend Testing leicht gemacht @EnterJS2018
 
Scala for Test Automation
Scala for Test AutomationScala for Test Automation
Scala for Test Automation
 
Token Testing Slides
Token  Testing SlidesToken  Testing Slides
Token Testing Slides
 
JavaFX8 TestFX - CDI
JavaFX8   TestFX - CDIJavaFX8   TestFX - CDI
JavaFX8 TestFX - CDI
 
Integration Testing With ScalaTest and MongoDB
Integration Testing With ScalaTest and MongoDBIntegration Testing With ScalaTest and MongoDB
Integration Testing With ScalaTest and MongoDB
 
Developers Testing - Girl Code at bloomon
Developers Testing - Girl Code at bloomonDevelopers Testing - Girl Code at bloomon
Developers Testing - Girl Code at bloomon
 
Unit testing JavaScript: Jasmine & karma intro
Unit testing JavaScript: Jasmine & karma introUnit testing JavaScript: Jasmine & karma intro
Unit testing JavaScript: Jasmine & karma intro
 
Testing Legacy Rails Apps
Testing Legacy Rails AppsTesting Legacy Rails Apps
Testing Legacy Rails Apps
 
JavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and KarmaJavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and Karma
 

Similar to Automated php unit testing in drupal 8

Testing
TestingTesting
Testing
Shweta Grover
 
Test in action – week 1
Test in action – week 1Test in action – week 1
Test in action – week 1Yi-Huan Chan
 
Principles and patterns for test driven development
Principles and patterns for test driven developmentPrinciples and patterns for test driven development
Principles and patterns for test driven development
Stephen Fuqua
 
Java 201 Intro to Test Driven Development in Java
Java 201   Intro to Test Driven Development in JavaJava 201   Intro to Test Driven Development in Java
Java 201 Intro to Test Driven Development in Java
agorolabs
 
Drupalcamp Simpletest
Drupalcamp SimpletestDrupalcamp Simpletest
Drupalcamp Simpletestlyricnz
 
Testing In Drupal
Testing In DrupalTesting In Drupal
Testing In Drupal
Ryan Cross
 
Staging Drupal 8 31 09 1 3
Staging Drupal 8 31 09 1 3Staging Drupal 8 31 09 1 3
Staging Drupal 8 31 09 1 3Drupalcon Paris
 
Breaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit Testing
Steven Smith
 
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
FalafelSoftware
 
Generalization in Auto-Testing. How we put what we had into new Technological...
Generalization in Auto-Testing. How we put what we had into new Technological...Generalization in Auto-Testing. How we put what we had into new Technological...
Generalization in Auto-Testing. How we put what we had into new Technological...
SQALab
 
Breaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit Testing
Steven Smith
 
Test Driven Development with JavaFX
Test Driven Development with JavaFXTest Driven Development with JavaFX
Test Driven Development with JavaFX
Hendrik Ebbers
 
Qt test framework
Qt test frameworkQt test framework
Qt test framework
ICS
 
Junit
JunitJunit
Into The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applicationsInto The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applications
Ortus Solutions, Corp
 
Infinum Android Talks #17 - Testing your Android applications by Ivan Kust
Infinum Android Talks #17 - Testing your Android applications by Ivan KustInfinum Android Talks #17 - Testing your Android applications by Ivan Kust
Infinum Android Talks #17 - Testing your Android applications by Ivan Kust
Infinum
 
Apache DeltaSpike
Apache DeltaSpikeApache DeltaSpike
Apache DeltaSpikeos890
 
Unit tests = maintenance hell ?
Unit tests = maintenance hell ? Unit tests = maintenance hell ?
Unit tests = maintenance hell ?
Thibaud Desodt
 
When assertthat(you).understandUnitTesting() fails
When assertthat(you).understandUnitTesting() failsWhen assertthat(you).understandUnitTesting() fails
When assertthat(you).understandUnitTesting() fails
Martin Skurla
 
Staying Sane with Drupal (A Develper's Survival Guide)
Staying Sane with Drupal (A Develper's Survival Guide)Staying Sane with Drupal (A Develper's Survival Guide)
Staying Sane with Drupal (A Develper's Survival Guide)
Oscar Merida
 

Similar to Automated php unit testing in drupal 8 (20)

Testing
TestingTesting
Testing
 
Test in action – week 1
Test in action – week 1Test in action – week 1
Test in action – week 1
 
Principles and patterns for test driven development
Principles and patterns for test driven developmentPrinciples and patterns for test driven development
Principles and patterns for test driven development
 
Java 201 Intro to Test Driven Development in Java
Java 201   Intro to Test Driven Development in JavaJava 201   Intro to Test Driven Development in Java
Java 201 Intro to Test Driven Development in Java
 
Drupalcamp Simpletest
Drupalcamp SimpletestDrupalcamp Simpletest
Drupalcamp Simpletest
 
Testing In Drupal
Testing In DrupalTesting In Drupal
Testing In Drupal
 
Staging Drupal 8 31 09 1 3
Staging Drupal 8 31 09 1 3Staging Drupal 8 31 09 1 3
Staging Drupal 8 31 09 1 3
 
Breaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit Testing
 
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
 
Generalization in Auto-Testing. How we put what we had into new Technological...
Generalization in Auto-Testing. How we put what we had into new Technological...Generalization in Auto-Testing. How we put what we had into new Technological...
Generalization in Auto-Testing. How we put what we had into new Technological...
 
Breaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit TestingBreaking Dependencies to Allow Unit Testing
Breaking Dependencies to Allow Unit Testing
 
Test Driven Development with JavaFX
Test Driven Development with JavaFXTest Driven Development with JavaFX
Test Driven Development with JavaFX
 
Qt test framework
Qt test frameworkQt test framework
Qt test framework
 
Junit
JunitJunit
Junit
 
Into The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applicationsInto The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applications
 
Infinum Android Talks #17 - Testing your Android applications by Ivan Kust
Infinum Android Talks #17 - Testing your Android applications by Ivan KustInfinum Android Talks #17 - Testing your Android applications by Ivan Kust
Infinum Android Talks #17 - Testing your Android applications by Ivan Kust
 
Apache DeltaSpike
Apache DeltaSpikeApache DeltaSpike
Apache DeltaSpike
 
Unit tests = maintenance hell ?
Unit tests = maintenance hell ? Unit tests = maintenance hell ?
Unit tests = maintenance hell ?
 
When assertthat(you).understandUnitTesting() fails
When assertthat(you).understandUnitTesting() failsWhen assertthat(you).understandUnitTesting() fails
When assertthat(you).understandUnitTesting() fails
 
Staying Sane with Drupal (A Develper's Survival Guide)
Staying Sane with Drupal (A Develper's Survival Guide)Staying Sane with Drupal (A Develper's Survival Guide)
Staying Sane with Drupal (A Develper's Survival Guide)
 

Recently uploaded

LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Product School
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
Ana-Maria Mihalceanu
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
Paul Groth
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 

Recently uploaded (20)

LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...Mission to Decommission: Importance of Decommissioning Products to Increase E...
Mission to Decommission: Importance of Decommissioning Products to Increase E...
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Monitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR EventsMonitoring Java Application Security with JDK Tools and JFR Events
Monitoring Java Application Security with JDK Tools and JFR Events
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMsTo Graph or Not to Graph Knowledge Graph Architectures and LLMs
To Graph or Not to Graph Knowledge Graph Architectures and LLMs
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 

Automated php unit testing in drupal 8

  • 2. • @jaypan on Drupal.org and drupal.stackexchange.com • Acquia certified Drupal Grandmaster • Acquia Certified Developer • Acquia Certified Developer – Front end specialist • Acquia Certified Developer – Back end specialist • Living in Japan since 2000 • Working with Drupal since 2007 • Currently writing a book on coding in Drupal JAY FRIENDLY
  • 3. JAY FRIENDLY • Owner of Jaypan in Yokohama • English https://www.jaypan.com • Japanese https://www.jaypan.jp • Jaypan specializes in • high-performance AJAX/JavaScript heavy applications • Custom module/theme development • App Development • Drupal integration with outside technologies • JavaScript libraries • Linux programs • Multiple Drupal instances • Technical consulting and translation (Japanese/English)
  • 4. OVERVIEW • Manual testing vs. automated testing • How do automated tests work? • History of testing in Drupal • Types of automated tests in Drupal • Discussion of each type of test
  • 5. MANUAL TESTING • Developer creates new functionality • Developer tests new functionality • Other people (client, team-members) test new functionality • Found bugs are fixed
  • 6. PROBLEMS WITH MANUAL TESTING • Complex functionality requires significant time to test • Easy to forget to test a given scenario • Changes to code require re-testing every scenario after any change • Changes to code in other parts of the system may break functionality without anyone realizing it • As time passes, the conditions that need to be tested may be forgotten or lost as project members change • Coming back to code after a period of time means the developer is not sure what is required
  • 7. AUTOMATED TESTING • Developer creates automated tests that run through all testing scenarios • Testing system runs all tests and reports any behaviors that are unexpected (aka bugs)
  • 8. BENEFITS OF AUTOMATED TESTING • Removes requirement of getting humans to test after each code change • Tests are faster than human testing • Tests for the whole system can be run before committing any code to a production server, to ensure new functionality in one part of the system hasn’t broken functionality in another part of the system. • If (or rather, when) new bugs are found, tests can be added along with the fix, to ensure that the bug never arises again • Overall system is more stable.
  • 9. PROBLEMS WITH AUTOMATED TESTING • Writing tests takes a significant amount of time • Extra time means extra development resources • Clients cannot directly see tests, and may not understand the extra costs in developing when there is nothing for them to look at. • Tests only test what they have been written to test. If the developer does not think to test something, a bug could exist. The existence of automated tests in such a case may create a false sense of security • Tests happen in a virtual (non-visible) environment, making them hard to debug (and even create) • Changing code often requires re-writing old tests
  • 10. OR IN OTHER WORDS, WHAT IS IT? HOW DOES A TEST WORK? • A test will first run some code, then do ‘assertions’ to ensure that the results work as expected. • For example, in Drupal 8, nodes have a method id(), that returns the ID of the node. • An automated test would create a new node object, then run $node->id() and test: • Was a result returned? • Is the result an integer? • Is the returned ID the expected (correct) ID of the node that was created? • Any failed assertions result in a failed test, telling the developer exactly what failed, so they know what to fix.
  • 11. AUTOMATED TESTING IN DRUPAL - HISTORY • Drupal 6 - Simpletest module • Drupal 7 - Simpletest in core • Drupal 8 - • Simpletest still in core but deprecated (to be removed in D9) • PHPUnit introduced into core • This presentation focuses exclusively on PHPUnit
  • 12. TYPES OF TESTS IN DRUPAL 8 • Unit tests • Kernel tests • Functional tests • JavaScript functional tests
  • 13. UNIT TESTS IN DRUPAL 8 • A Unit is a PHP Class (OOP) • Unit tests test code, not user-end functionality • Unit tests will test a single class, to ensure that it behaves as expected • Unit tests test the unit (class) alone in isolation, rather than in the Drupal environment • Unit testing is very fast, as the system does not need to be bootstrapped to run the tests
  • 14. UNIT TEST OVERVIEW • Unit tests go in [MODULENAME]/tests/src/Unit • Namespace is DrupalTests[MODULENAME]Unit • Unit tests extend DrupalTestsUnitTestCase • Annotation should include (at a minimum) • Class: • @coversDefaultClass - the class the the test is testing • @group - the group of tests (usually module name) that the test belongs to • Method: • @covers - the method of the class being tested
  • 15. UNIT TEST OVERVIEW • Testing class name should end in ‘Test’ • Example: the test for the Person class would be PersonTest • Testing method (function) name must start with ‘test’ • Example: the method used to test the id() method of Person would be testId()
  • 16. UNIT TEST OVERVIEW • Test classes can have a setUp() method, that sets up conditions/objects before each test in the test class. • Useful when all tests require a similar set of conditions • Test classes can also have a tearDown() method, where objects are cleaned up after each test in the test class. • Test classes can have a setUpBeforeClass() method that is run once before the test class is run • Test classes can have a tearDownAfterClass() method that is run once after the test class is finished
  • 17. CLASS (UNIT) TO BE TESTED class Person { private $id; public function setId($id) { $this->id = $id; } public function id() { return $this->id(); } }
  • 18. EXAMPLE UNIT TEST FOR PERSON CLASS /** * @coversDefaultClass Person */ class PersonTest { /** * @covers ::setId() */ public testId() { $person = new Person(); $person->setId(123); $this->assertNotNull($person->getId(), ‘A value is returned for the ID’); $this->assertTrue(is_int($person->id()), ‘The returned ID is an integer’); $this->assertEqual(123, $this->getId(), ‘The returned ID is correct’); } }
  • 19. KERNEL TESTS KERNEL TESTS IN DRUPAL 8 • Kernel tests are executed in a limited Drupal environment, similar to the environment before Drupal has been installed • Tests can access files and the database • Declared modules are loaded, but not installed. Installation needs to be done manually • Kernel tests are slower than Unit tests, but faster than Functional tests
  • 20. KERNEL TEST OVERVIEW • Kernel tests go in [MODULENAME]/tests/src/Kernel • Namespace is DrupalTests[MODULENAME]Kernel • Kernel tests extend DrupalTestsKernelTestBast
  • 22. FUNCTIONAL TESTS IN DRUPAL 8 • Functional tests work in an installed Drupal environment • Modules needed for the test need to be declared for installation • Tests use an internal browser (aka code - not visual), where links can be clicked, form elements can be filled out, and pages can be requested, all programmatically • Users, roles, blocks and other entities can be created • Tests are slow as each test builds a new Drupal instance in which to run the tests
  • 23. FUNCTIONAL TEST OVERVIEW • Unit tests go in [MODULENAME]/tests/src/Functional • Namespace is DrupalTests[MODULENAME]Functional • Unit tests extend DrupalTestsBrowserTestBase • Annotation should include (at a minimum) • Class: • @group - the group of tests (usually module name) that the test belongs to • $modules property contains an array of modules to be installed • example: $modules = [‘node’, ‘restrict_ip’]
  • 24. FROM RESTRICT IP MODULE EXAMPLE FUNCTIONAL TEST namespace DrupalTestsrestrict_ipFunctional; /** * @group restrict_ip */ class RestrictIpAccessTest extends RestrictIpBrowserTestBase // which extends BrowserTestBase { protected static $modules = ['restrict_ip', 'node']; /** * Test that a user is blocked when the module is enabled */ public function testModuleEnabled() { $adminUser = $this->drupalCreateUser(['administer restricted ip addresses', 'access administration pages', 'administer modules']); $this->drupalLogin($adminUser); $this->drupalGet('admin/config/people/restrict_ip'); $this->assertStatusCodeEquals(200); $this->checkCheckbox('#edit-enable'); $this->click('#edit-submit'); $this->assertSession()->pageTextContains('The page you are trying to access cannot be accessed from your IP address.'); } }
  • 25. JAVASCRIPT FUNCTIONAL TESTS IN DRUPAL 8 • JavaScript Functional tests work in a fully installed Drupal environment • Extends Functional tests to include JavaScript • Require PhantomJS browser • Virtual browser (no GUI) • Requires installation on testing computer/server • Can test #ajax methods, as well as other JavaScript functionality
  • 26. JAVASCRIPT FUNCTIONAL TEST OVERVIEW • Unit tests go in [MODULENAME]/tests/src/FunctionalJavascript • Namespace is DrupalTests[MODULENAME]FunctionalJavascript • Unit tests extend DrupalFunctionalJavascriptTestsJavascriptTestBase • Annotation should include (at a minimum) • Class: • @group - the group of tests (usually module name) that the test belongs to
  • 27. FROM FANCY LOGIN MODULE EXAMPLE JAVASCRIPT FUNCTIONAL TEST namespace DrupalTestsfancy_loginFunctionalJavascript; /** * @group fancy_login */ class FancyLoginJavascriptTest extends FancyLoginJavascriptTestBase // which extends JavascriptTestBase { public function testDimmerBackgroundColor() { $this->openPopup(); $this->assertJsCondition('jQuery("#fancy_login_dim_screen").not(":animated")'); $this->assertJsCondition('jQuery("#fancy-login-user-login-form").not(":animated")'); $script = $this->createScriptString('#fancy_login_dim_screen', 'background-color', 'rgb(255, 255, 255)'); if(!$this->getSession()->evaluateScript($script)) { throw new Exception('background-color is not white'); } // … } }
  • 28. WRITE TESTS THEN CODE TESTING DRIVEN DEVELOPMENT • Write tests as the start of the project, rather than the end • Developers build and test as they go • Gives a clear indicator of what to do next • Gives a clear indication of when the project is complete • Note - testing driven development is purely theoretical for me - I’ve never done it
  • 29. WHERE TO GO FROM HERE • PHPUnit in Drupal 8 - https://www.drupal.org/docs/8/phpunit • Documentation is limited • Lots and lots of Googling