Actions

Testing Area/Behat Testing/Basics: Difference between revisions

From Mahara Wiki

< Testing Area‎ | Behat Testing
No edit summary
No edit summary
 
(11 intermediate revisions by 3 users not shown)
Line 1: Line 1:
[[Category:Behat]]
[[Category:Behat]]
<h1>How to write a Behat test</h1>
<h1>How to write a Behat test - Overview/Summary</h1>


Confident coding? Skip to here:[http://docs.behat.org/en/v2.5/guides/1.gherkin.html]
Behat tests are written in Gerkin language, which is English readable. You write the test in a text editor like Vim, Gedit, Atom or VS Code. For beginners, Gedit is recommended.
 
Behat tests are written in Gerkin language, which is English readable. You write the test in a text editor like Vim, Gedit or Eclipse. For beginners, Gedit is reccomended. Open your editor.
To write the test start with the feature. The purpose of the Feature is to say what you are testing and what you gain from testing that feature.  
To write the test start with the feature. The purpose of the Feature is to say what you are testing and what you gain from testing that feature.  


'''@javascript @core @core_portfolio_page''' <- (Grouping the tests to run them. Tag them appropriately)
  ''<@javascript @core @core_portfolio_page>''         # Tag the tests appropriately to allow them to be run selectively.
   '''Feature:''' Creating a Portfolio page <- (The point of the test)
   '''Feature:''' ''<Portfolio page creation>''              # Identify the area under test.
   '''In order to''' have a portfolio page in Mahara <- (What you are wanting to do)
   '''As a''' ''<logged in account type>''                    # Identify who 'you' are  
   '''As an admin''' <- (Who you are logging in as eg student, admin, institution admin)
                                                          (e.g. site admin, institution admin, tutor, student).
   '''So I can''' save my work on my Portfolio page <- (How you benefit from it)
   '''I want to''' ''<save a portfolio page in Mahara>''      # Identify what 'you' want to do.                                 
   '''So that''' ''<there is a record of my work>''  # Identify why 'you' want to do it (i.e. benefit from it).


The Scenario states what you are doing to test that feature. If you are testing a specific bug add the bug number here from Launchpad.


  '''Scenario''': Creating a Portfolio Page (Bug 1234) <- (Describe what you are about to do to test it works)
Then follows the scenario/s, there can be many of these, each addressing different preconditions. Their names should identify what aspect of the feature is being tested. If you are testing a specific bug add the bug number here from Launchpad.
  '''Given''' (Setting the scene, these are you pre conditions)
  '''#''' Comment (Comments are milestone markers to make it human readable)
  '''And''' (Use as many And's as you need to set the pre conditions)
  '''When''' (This is an action you do like clicking or pressing a button)
  '''And'''  (Use as many And's as you need to describe the action you are doing like pushing 10 buttons to get to the place you want)
  '''#''' Comment (Adding a comment here to make it readable, describing what I am doing) 
  '''Then''' (This is the achieveable outcome. Often it's what you see when the link you clicked worked.)
  '''And'''  (Use as many And's as you need)


Write down every link you click and every button you pushed to create the page using the specific Behat syntax from this page [https://wiki.mahara.org/index.php/Testing/Behat_Testing/Steps].   
  '''Scenario:''' ''<Create basic page (Bug 761234)>''      # Identify what aspect/s of the feature that is being testing.
These are you Given, And, When and Then steps.
  ''#'' ''<Comment/s>''                                        # Comments may be used to clarify terminology e.g. what is 'basic'.
  '''Given''' ''<Set the scene with a condition...>''        # Identify the preconditions that need to be met.
  ''And''  ''<Add further condition/s>''                        # Use as many 'And's as you need to set the required preconditions.
  ''#''    ''<Comment/s>''                                      # Comments may be milestone markers to improve human readability.
  '''When'''  ''<Provable action occurs...>''                # Identify something that happens like clicking or pressing a button.
  ''And''  ''<Add further action/s to occur>''                  # Use as many 'And's as you need to describe the action/s
                                                                  (eg like pushing 10 buttons to get to the place you want).
  ''#''    ''<Comment/s>''                                      # Comments may be used to describing why something has to happen. 
  '''Then'''  ''<Add the desired outcome...>''              # Identify the achievable outcome e.g. what you see when you click.
  ''And''  ''<Add further desire outcome/s<''                  # Use as many 'And's as you need to definite success.
  ''#''    ''<Comment/s>''                                      # Comments may be used to further explain outcome/s & implications.
 
Write down every link you click and every button you pushed to create the page using the specific Behat syntax from this page [https://wiki.mahara.org/index.php/Testing/Behat_Testing/Steps].  These are your Given, And, When and Then steps.


Behat test example:
Behat test example:
<pre>
  Feature: Portfolio page creation
  As a student
  I want to save a portfolio page in Mahara
  So that there is a record of my work to share.


  Feature: Creating a Portfolio page
  Scenario: Create basic page (Bug 761234)
  In order to have a Portfolio Page in Mahara
   Given I am logged in as the user "Student1" with password "Password-123"
  As an admin
   And I follow "Portfolio"
  So I can save my work on my Portfolio page
   # Create a page  
 
  Scenario: Creating a Portfolio page (Bug 1234)
   Given I am logged in as the user "admin" with password "Password1"
   And I follow "Porfolio"
   # Creating a page  
   And I follow "Pages"
   And I follow "Pages"
   When I press "Create page"
   When I press "Create page"
   # Filling in the information on the page
   # Filling in the information on the page
   And I fill in the following:
   And I fill in the following:
   | Page title | This is where you put the title of the page |
   | Page title | This is where you put the page title |
   | Page description | This is where you type the description of the page |
   | Page description | This is where you type the description of the page |
   # Saving the page
   # Saving the page
Line 51: Line 52:
   # Verifying that the page saved   
   # Verifying that the page saved   
   Then I should see "This is where you put the page title"
   Then I should see "This is where you put the page title"
</pre>
'''Save the test as a .feature file in the appropriate directory. For example, portfolio_page_creation.feature'''
The fastest way to write a test is using parts from previous tests. In the terminal use this command to search for the specific steps that you need:
  grep --color=auto -RniP -C 3 --include *.feature '(given|when|then|and).*delete.*activity' *
Replace delete and activity with the specific things that you need to search for


'''Save the test as a .feature file in the right directory. For example, portfolio_page.feature'''
For further information on Behat, Gherkin etc. please see [http://docs.behat.org/en/v2.5/guides/1.gherkin.html here] (the base web site for Behat testing). Also, see [https://cucumber.io/docs/guides/ here] for more information on Gherkin and Cucumber.

Latest revision as of 15:50, 7 August 2020

How to write a Behat test - Overview/Summary

Behat tests are written in Gerkin language, which is English readable. You write the test in a text editor like Vim, Gedit, Atom or VS Code. For beginners, Gedit is recommended. To write the test start with the feature. The purpose of the Feature is to say what you are testing and what you gain from testing that feature.

 <@javascript @core @core_portfolio_page>         # Tag the tests appropriately to allow them to be run selectively.
 Feature: <Portfolio page creation>               # Identify the area under test.
 As a <logged in account type>                    # Identify who 'you' are 
                                                         (e.g. site admin, institution admin, tutor, student).
 I want to <save a portfolio page in Mahara>      # Identify what 'you' want to do.                                  
 So that <there is a record of my work>  # Identify why 'you' want to do it (i.e. benefit from it).


Then follows the scenario/s, there can be many of these, each addressing different preconditions. Their names should identify what aspect of the feature is being tested. If you are testing a specific bug add the bug number here from Launchpad.

 Scenario: <Create basic page (Bug 761234)>       # Identify what aspect/s of the feature that is being testing.
 # <Comment/s>                                         # Comments may be used to clarify terminology e.g. what is 'basic'.
 Given <Set the scene with a condition...>        # Identify the preconditions that need to be met.
 And  <Add further condition/s>                        # Use as many 'And's as you need to set the required preconditions.
 #    <Comment/s>                                      # Comments may be milestone markers to improve human readability.
 When  <Provable action occurs...>                # Identify something that happens like clicking or pressing a button.
 And  <Add further action/s to occur>                  # Use as many 'And's as you need to describe the action/s
                                                                 (eg like pushing 10 buttons to get to the place you want).
 #    <Comment/s>                                      # Comments may be used to describing why something has to happen.  
 Then  <Add the desired outcome...>               # Identify the achievable outcome e.g. what you see when you click.
 And  <Add further desire outcome/s<                   # Use as many 'And's as you need to definite success. 
 #    <Comment/s>                                      # Comments may be used to further explain outcome/s & implications.
 

Write down every link you click and every button you pushed to create the page using the specific Behat syntax from this page [1]. These are your Given, And, When and Then steps.

Behat test example:

  Feature: Portfolio page creation
  As a student
  I want to save a portfolio page in Mahara
  So that there is a record of my work to share.

 Scenario: Create basic page (Bug 761234)
  Given I am logged in as the user "Student1" with password "Password-123"
  And I follow "Portfolio"
  # Create a page 
  And I follow "Pages"
  When I press "Create page"
  # Filling in the information on the page
  And I fill in the following:
  | Page title | This is where you put the page title |
  | Page description | This is where you type the description of the page |
  # Saving the page
  And I press "Save"
  And I press "Done"
  # Verifying that the page saved  
  Then I should see "This is where you put the page title"

Save the test as a .feature file in the appropriate directory. For example, portfolio_page_creation.feature

The fastest way to write a test is using parts from previous tests. In the terminal use this command to search for the specific steps that you need:

 grep --color=auto -RniP -C 3 --include *.feature '(given|when|then|and).*delete.*activity' *

Replace delete and activity with the specific things that you need to search for

For further information on Behat, Gherkin etc. please see here (the base web site for Behat testing). Also, see here for more information on Gherkin and Cucumber.