Actions

Difference between revisions of "Testing Area/Behat Testing/Basics"

From Mahara Wiki

< Testing Area‎ | Behat Testing
m
(19 intermediate revisions by the same user not shown)
Line 4: Line 4:
 
Confident coding? Skip to here:[http://docs.behat.org/en/v2.5/guides/1.gherkin.html]
 
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 or Eclipse. For beginners, Gedit is reccomended.  
+
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 recommended. Open your editor.
To write the test start with the feature, you are about to click around Mahara and test an area so say:
+
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.
 +
  
   Feature: Creating a Portfolio page <- (the point of the test)
+
'''@javascript @core @core_portfolio_page'''            # Grouping the tests to run them. Tag them appropriately
   In order to have a portfolio page in Mahara <- (What you are wanting to do)
+
   '''Feature:''' Creating a Portfolio page               # The point of the test
   As a student <- (Who you are logging in as eg student, admin, institution admin)
+
   '''In order to''' have a portfolio page in Mahara     # What you are wanting to do
   So I can save my work on my Portfolio page <- (How you benefit from it)
+
   '''As an admin'''                                      # Who you are logging in as eg student, admin, institution admin
 +
   '''So I can''' save my work on my Portfolio page       # How you 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
   Scenario: Creating a Portfolio page <- (Describe what you are about to do to test it works)
+
   '''Given''' (Setting the scene, these are you pre conditions)
   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)
+
  '''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)
+
   '''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)
+
   '''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)
   Then (This is the achieveable outcome. Often it's what you see when the link you clicked worked.)  
+
   '''#''' Comment (Adding a comment here to make it readable, describing what I am doing) 
   And  (Use as many And's as you need)  
+
  '''Then''' (This is the achievable 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].   
 
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 you Given, And, When and Then steps.
+
These are your Given, And, When and Then steps.
  
 
Behat test example:
 
Behat test example:
 
+
<pre>
   Feature: Creating a Portfolio page
+
   Feature: Creating a Portfolio page  
 
   In order to have a Portfolio Page in Mahara
 
   In order to have a Portfolio Page in Mahara
   As a student
+
   As an admin
 
   So I can save my work on my Portfolio page
 
   So I can save my work on my Portfolio page
  
  Scenario: Creating a Portfolio page  
+
  Scenario: Creating a Portfolio page (Bug 1234)
 
   Given I am logged in as the user "admin" with password "Password1"
 
   Given I am logged in as the user "admin" with password "Password1"
   And I follow "Porfolio"
+
   And I follow "Portfolio"
 +
  # 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
 
   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 title of the page |
 
   | 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
 
   And I press "Save"
 
   And I press "Save"
   And I press "Done"   
+
   And I press "Done"
 +
  # 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 right directory. For example, portfolio_page.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

Revision as of 15:45, 6 March 2015

How to write a Behat test

Confident coding? Skip to here:[1]

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 recommended. 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.


@javascript @core @core_portfolio_page            # Grouping the tests to run them. Tag them appropriately
 Feature: Creating a Portfolio page               # The point of the test
 In order to have a portfolio page in Mahara      # What you are wanting to do
 As an admin                                      # Who you are logging in as eg student, admin, institution admin
 So I can save my work on my Portfolio page       # How you 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
 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 achievable 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 [2]. These are your Given, And, When and Then steps.

Behat test example:

  Feature: Creating a Portfolio page 
  In order to have a Portfolio Page in Mahara
  As an admin
  So I can save my work on my Portfolio page

 Scenario: Creating a Portfolio page (Bug 1234)
  Given I am logged in as the user "admin" with password "Password1"
  And I follow "Portfolio"
  # Creating 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 title of the page |
  | 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 right directory. For example, portfolio_page.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