SlideShare une entreprise Scribd logo
1  sur  16
Simpletest
Testing in Drupal - A beginners guide
What is Simpletest?


Contributed module available for Drupal 6 (requires
patch), Pressflow & Drupal 7
Enables developers to test small pieces of PHP code
(Unit testing)
Enables developers to test web-oriented functionality
(Functional testing)
Why test code?


Testing ensures your code works when you’ve
finished working on it
Tests can be re-run in the future when code starts
interacting with your code
We can run tests continually when we commit new
code
Writing tests makes you think about your code
Testing improves overall code quality.
Unit & Functional tests in Simpletest


 Simpletest provides two classes
   DrupalWebTestCase – For functional testing
   DrupalUnitTestCase - For unit testing
 Both contain methods that save us time when writing
 tests
 We’ll look at the two cases in more detail in this
 presentation
Functional Testing


Simpletest create a complete Drupal installation
It runs tests on a virtual web browser acting like you would
by hand
Each test runs in a new site instance
No modules, users, nodes, content types exist unless you
specify.
Simpletest Example
              preparation – step 1

Begin by creating a file called simpletest_example.test and put
it in the module directory.




Create a class that extends DrupalWebTestCase
Declare a user that we’ll use to interact with the browser
Preparation – step 2




We need to make information available to the
Simpletest testing interface – use getInfo
Preparation – step 3




The setUp function is used to customise the environment you will be working
in.
Enable modules by calling parent::setUp(‘MODULENAME’)
Create a user and set permissions using drupalCreateUser method
In this example we enable a module that defines a simple node and a
permission set to create and edit that node
A basic test

Key part of tests are the API functions and assertions.
Below we use “drupalPost” to post through values to the node
creation page
We then assert that a success message should be returned using
the “assertText” assertion
A more detailed test
                  Create a node using
                  DrupalCreateNode
                  Print out any debug
                  information using
                  “verbose” method
                  Navigate to the node
                  edit page using
                  “drupalGet”
                  Make sure we can
                  access the page ok
                  using
                  “assertResponse”
                  Check the node title is
                  on the page using
                  “assertText”
Unit Testing in Simpletest


Simpletest offers very powerful functionality through
its DrupalWebTestCase, but there may be times when
you want to write a traditional, simple unit test.
Test only isolated pieces of functionality
Make sure to test a variety of conditions
Use “DrupalUnitTestCase”
What to test?




This is a good example of a function to test
It has no dependencies and is only a few lines of code
Pass in different values for $date_string and check the
returned Boolean value
Example test
         Preparation – step 1


Create a .test file and add the path to your modules
.info file

Setup the test case in a similar way to the functional
tests but using “DrupalUnitTestCase”
Preparation – step 2


Just like our DrupalWebTestCase we need to
implement the getInfo method
And finally a test…

                 Call the function with
                 various different values
                 Create a message
                 string to be displayed
                 in case of failure
                 Make basic assertions
                 on the returned value
                 These tests should be
                 quick and simple to
                 write
Reading



http://drupal.org/simpletest-tutorial
http://drupal.org/node/811254
http://www.lullabot.com/articles/introduction-unit-
testing

Contenu connexe

Tendances

Testing - Selenium? Rich-Clients? Containers?
Testing - Selenium? Rich-Clients? Containers?Testing - Selenium? Rich-Clients? Containers?
Testing - Selenium? Rich-Clients? Containers?
Tobias Schneck
 

Tendances (19)

Jenkins tutorial
Jenkins tutorialJenkins tutorial
Jenkins tutorial
 
D3_Tuto_GD
D3_Tuto_GDD3_Tuto_GD
D3_Tuto_GD
 
React in production
React in productionReact in production
React in production
 
Mete Atamel
Mete AtamelMete Atamel
Mete Atamel
 
Jenkins tutorial for beginners
Jenkins tutorial for beginnersJenkins tutorial for beginners
Jenkins tutorial for beginners
 
High Performance NodeJS
High Performance NodeJSHigh Performance NodeJS
High Performance NodeJS
 
Php unit for drupal 8
Php unit for drupal 8Php unit for drupal 8
Php unit for drupal 8
 
Introduction to React JS
Introduction to React JSIntroduction to React JS
Introduction to React JS
 
React context
React context  React context
React context
 
Jenkins CI/CD setup for iOS in Mac OSX
Jenkins CI/CD setup for iOS in Mac OSXJenkins CI/CD setup for iOS in Mac OSX
Jenkins CI/CD setup for iOS in Mac OSX
 
Riga Dev Day - Automated Android Continuous Integration
Riga Dev Day - Automated Android Continuous IntegrationRiga Dev Day - Automated Android Continuous Integration
Riga Dev Day - Automated Android Continuous Integration
 
Gradle - the Enterprise Automation Tool
Gradle  - the Enterprise Automation ToolGradle  - the Enterprise Automation Tool
Gradle - the Enterprise Automation Tool
 
Debugging Javascript
Debugging JavascriptDebugging Javascript
Debugging Javascript
 
JavaScript + Jenkins = Winning!
JavaScript + Jenkins = Winning!JavaScript + Jenkins = Winning!
JavaScript + Jenkins = Winning!
 
Testing - Selenium? Rich-Clients? Containers?
Testing - Selenium? Rich-Clients? Containers?Testing - Selenium? Rich-Clients? Containers?
Testing - Selenium? Rich-Clients? Containers?
 
Jenkins Pipeline 101 and TCI - presentation and workshop
Jenkins Pipeline 101 and TCI - presentation and workshopJenkins Pipeline 101 and TCI - presentation and workshop
Jenkins Pipeline 101 and TCI - presentation and workshop
 
Baking docker using chef
Baking docker using chefBaking docker using chef
Baking docker using chef
 
Writing a Jenkins / Hudson plugin
Writing a Jenkins / Hudson pluginWriting a Jenkins / Hudson plugin
Writing a Jenkins / Hudson plugin
 
201502 - Integration Testing
201502 - Integration Testing201502 - Integration Testing
201502 - Integration Testing
 

En vedette (6)

Auditing Drupal Sites
Auditing Drupal SitesAuditing Drupal Sites
Auditing Drupal Sites
 
CTools – toolbox for developers. Yuri Gerasimov.
CTools – toolbox for developers. Yuri Gerasimov.CTools – toolbox for developers. Yuri Gerasimov.
CTools – toolbox for developers. Yuri Gerasimov.
 
Lean Analytics: A short summary
Lean Analytics: A short summaryLean Analytics: A short summary
Lean Analytics: A short summary
 
A swift introduction to Swift
A swift introduction to SwiftA swift introduction to Swift
A swift introduction to Swift
 
The Ultimate Guide to Startup Marketing
The Ultimate Guide to Startup MarketingThe Ultimate Guide to Startup Marketing
The Ultimate Guide to Startup Marketing
 
The Beginners Guide to Startup PR #startuppr
The Beginners Guide to Startup PR #startupprThe Beginners Guide to Startup PR #startuppr
The Beginners Guide to Startup PR #startuppr
 

Similaire à Simpletest - A beginners guide

Drupalcamp Simpletest
Drupalcamp SimpletestDrupalcamp Simpletest
Drupalcamp Simpletest
lyricnz
 
Jquery Plugin
Jquery PluginJquery Plugin
Jquery Plugin
Ravi Mone
 
Testing the Enterprise Layers - the A, B, C's of Integration Testing - Aslak ...
Testing the Enterprise Layers - the A, B, C's of Integration Testing - Aslak ...Testing the Enterprise Layers - the A, B, C's of Integration Testing - Aslak ...
Testing the Enterprise Layers - the A, B, C's of Integration Testing - Aslak ...
JAXLondon2014
 

Similaire à Simpletest - A beginners guide (20)

Drupalcamp Simpletest
Drupalcamp SimpletestDrupalcamp Simpletest
Drupalcamp Simpletest
 
Testing In Drupal
Testing In DrupalTesting In Drupal
Testing In Drupal
 
Jquery Plugin
Jquery PluginJquery Plugin
Jquery Plugin
 
Gallio Crafting A Toolchain
Gallio Crafting A ToolchainGallio Crafting A Toolchain
Gallio Crafting A Toolchain
 
Features java9
Features java9Features java9
Features java9
 
Testing
TestingTesting
Testing
 
Presentation
PresentationPresentation
Presentation
 
Selenium-Webdriver With PHPUnit Automation test for Joomla CMS!
Selenium-Webdriver With PHPUnit Automation test for Joomla CMS!Selenium-Webdriver With PHPUnit Automation test for Joomla CMS!
Selenium-Webdriver With PHPUnit Automation test for Joomla CMS!
 
Modular programming Using Object in Scala
Modular programming Using Object in ScalaModular programming Using Object in Scala
Modular programming Using Object in Scala
 
Neo4j Stored Procedure Training Part 1
Neo4j Stored Procedure Training Part 1Neo4j Stored Procedure Training Part 1
Neo4j Stored Procedure Training Part 1
 
SKILLWISE_SELENIUM
SKILLWISE_SELENIUMSKILLWISE_SELENIUM
SKILLWISE_SELENIUM
 
Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to java
 
Laravel Unit Testing
Laravel Unit TestingLaravel Unit Testing
Laravel Unit Testing
 
Stopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under TestStopping the Rot - Putting Legacy C++ Under Test
Stopping the Rot - Putting Legacy C++ Under Test
 
Testing in AngularJS
Testing in AngularJSTesting in AngularJS
Testing in AngularJS
 
Codeception
CodeceptionCodeception
Codeception
 
Anatomy of a Build Pipeline
Anatomy of a Build PipelineAnatomy of a Build Pipeline
Anatomy of a Build Pipeline
 
Testing the Enterprise Layers - the A, B, C's of Integration Testing - Aslak ...
Testing the Enterprise Layers - the A, B, C's of Integration Testing - Aslak ...Testing the Enterprise Layers - the A, B, C's of Integration Testing - Aslak ...
Testing the Enterprise Layers - the A, B, C's of Integration Testing - Aslak ...
 
Testing the Enterprise layers, with Arquillian
Testing the Enterprise layers, with ArquillianTesting the Enterprise layers, with Arquillian
Testing the Enterprise layers, with Arquillian
 
L2624 labriola
L2624 labriolaL2624 labriola
L2624 labriola
 

Dernier

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Enterprise Knowledge
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Dernier (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 

Simpletest - A beginners guide

  • 1. Simpletest Testing in Drupal - A beginners guide
  • 2. What is Simpletest? Contributed module available for Drupal 6 (requires patch), Pressflow & Drupal 7 Enables developers to test small pieces of PHP code (Unit testing) Enables developers to test web-oriented functionality (Functional testing)
  • 3. Why test code? Testing ensures your code works when you’ve finished working on it Tests can be re-run in the future when code starts interacting with your code We can run tests continually when we commit new code Writing tests makes you think about your code Testing improves overall code quality.
  • 4. Unit & Functional tests in Simpletest Simpletest provides two classes DrupalWebTestCase – For functional testing DrupalUnitTestCase - For unit testing Both contain methods that save us time when writing tests We’ll look at the two cases in more detail in this presentation
  • 5. Functional Testing Simpletest create a complete Drupal installation It runs tests on a virtual web browser acting like you would by hand Each test runs in a new site instance No modules, users, nodes, content types exist unless you specify.
  • 6. Simpletest Example preparation – step 1 Begin by creating a file called simpletest_example.test and put it in the module directory. Create a class that extends DrupalWebTestCase Declare a user that we’ll use to interact with the browser
  • 7. Preparation – step 2 We need to make information available to the Simpletest testing interface – use getInfo
  • 8. Preparation – step 3 The setUp function is used to customise the environment you will be working in. Enable modules by calling parent::setUp(‘MODULENAME’) Create a user and set permissions using drupalCreateUser method In this example we enable a module that defines a simple node and a permission set to create and edit that node
  • 9. A basic test Key part of tests are the API functions and assertions. Below we use “drupalPost” to post through values to the node creation page We then assert that a success message should be returned using the “assertText” assertion
  • 10. A more detailed test Create a node using DrupalCreateNode Print out any debug information using “verbose” method Navigate to the node edit page using “drupalGet” Make sure we can access the page ok using “assertResponse” Check the node title is on the page using “assertText”
  • 11. Unit Testing in Simpletest Simpletest offers very powerful functionality through its DrupalWebTestCase, but there may be times when you want to write a traditional, simple unit test. Test only isolated pieces of functionality Make sure to test a variety of conditions Use “DrupalUnitTestCase”
  • 12. What to test? This is a good example of a function to test It has no dependencies and is only a few lines of code Pass in different values for $date_string and check the returned Boolean value
  • 13. Example test Preparation – step 1 Create a .test file and add the path to your modules .info file Setup the test case in a similar way to the functional tests but using “DrupalUnitTestCase”
  • 14. Preparation – step 2 Just like our DrupalWebTestCase we need to implement the getInfo method
  • 15. And finally a test… Call the function with various different values Create a message string to be displayed in case of failure Make basic assertions on the returned value These tests should be quick and simple to write