Robot Framework Coding Style Guide
User Manual:
Open the PDF directly: View PDF .
Page Count: 16
Download | |
Open PDF In Browser | View PDF |
ROBOT FRAMEWORK CODING STYLE GUIDE Version 1.01 www.ngahr.com Robot Framework(RF) Coding Style Guide Document Control Document Information INFORMATION Document Id Document Owner Issue Date Last Saved Date File Name Roberto Galman 13-Apr-2015 13-Apr-2015 Robot_Framework_Coding_StyleGuide.docx Document History VERSION 1.00 1.01 ISSUE DATE 13-Apr-2015 28-Apr-2015 CHANGES Initial Doc Added Pre-condition and Post-Condition (Section 4.3) Document Approvals ROLE NAME SIGNATURE DATE Project Sponsor Project Review Group Project Manager Quality Manager (if applicable) Procurement Manager (if applicable) Communications Manager (if applicable) Project Office Manager (if applicable) Proprietary and Confidential to NGA Human Resources Page i Robot Framework(RF) Coding Style Guide TABLE OF CONTENTS 1 2 3 4 5 6 7 8 9 INTRODUCTION .................................................................................................................................. 3 FOLDER STRUCTURE & FILE ORGANIZATION ...................................................................................... 4 2.1 Test Automation Framework ................................................................................................... 4 2.2 Test Suite ................................................................................................................................ 4 CODE LAYOUT .................................................................................................................................. 5 3.1 Indentation & Max Line Length ............................................................................................... 5 3.2 Source File Encoding .............................................................................................................. 5 3.3 String Quotes .......................................................................................................................... 5 3.4 Whitespace in Expressions & Statements .............................................................................. 5 3.5 Comments ............................................................................................................................... 6 TEST SUITE STRUCTURE.................................................................................................................... 7 4.1 Test Data Table Names .......................................................................................................... 7 4.2 Settings Table ......................................................................................................................... 7 4.3 Pre-condition and Post-Condition ........................................................................................... 8 TEST CASE STRUCTURE .................................................................................................................... 9 5.1 Tags ........................................................................................................................................ 9 5.2 Abstraction Level ..................................................................................................................... 9 5.3 TestCase Structure ............................................................................................................... 10 USER KEYWORDS ........................................................................................................................... 11 6.1 General .................................................................................................................................. 11 6.2 Comments ............................................................................................................................. 11 6.3 Using Keywords .................................................................................................................... 11 VARIABLES ..................................................................................................................................... 12 7.1 Naming .................................................................................................................................. 12 7.2 Assigning Variable value ....................................................................................................... 12 REFERENCES .................................................................................................................................. 13 APPENDIX ....................................................................................................................................... 14 9.1 Definitions .............................................................................................................................. 14 Proprietary and Confidential to NGA Human Resources Page ii Robot Framework(RF) Coding Style Guide 1 INTRODUCTION This document discusses a set of guidelines for writing Robot Framework (RF) Test Scripts including Naming Conventions, Documentation, Test Suite/Case Structure, User Keywords, and Variables. The purpose of this document is to create a readable, accurate, stable & maintainable RF Test Suite. Proprietary and Confidential to NGA Human Resources Page 3 Robot Framework(RF) Coding Style Guide 2 FOLDER STRUCTURE & FILE ORGANIZATION Robot Framework Test Data is defined in tabular format using HTML, tab-separated values (TSV), plain text, or reStructuredText (reST) formats. It is recommended to use plain text format (.txt) with tabs used as separator. 2.1 T EST A UTOMATION F RAMEWORK Robot Framework allows you to create or extend the Test Library to add new functionality. This should be designed such that it can easily be integrated to a Test Suite by importing it. As Robot Framework is written in Python (.py), it is suggested that user library is implemented in Python as well. It should support: Command-line execution with parameterization Unit Tests available in the repository to be used for quick checks & testing updates Generates test execution logs with log-levels The recommended folder structure should be: + -_automationfwk - + src - - - - - - - */* - - - unit tests - - sw_dependencies.txt 2.2 T EST S UITE When creating the Test Suite Folder structure to be committed in GitLab, this should be consistent with the structure in ALM. This is for the purpose of traceability & mapping the test cases. ALM vs Gitab Repository The Test Suite Folder should follow the hierarchy: Module > Submodule > Functionality>*. The naming convention should be consistent with ALM. It may follow a CamelCase format but for long names, it can also be separated by underscores to improve readability. The same convention can be implemented for file names. _ _ _ _By_ e.g. FWK_ContextualAction_ContextualActionsDisplay_By_MGR Proprietary and Confidential to NGA Human Resources Page 4 Robot Framework(RF) Coding Style Guide 3 CODE LAYOUT 3.1 I NDENTATION & M AX L INE L ENGTH Use tabs per indentation level. If there is more data than readily fits the line, you may use ellipsis (...) to continue the previous line. The maximum line length is 120. Limiting the required editor window width makes it possible to have several files open side-by-side, and works well when using code review tools that present the two versions in adjacent columns (PEP-8). 3.2 S OURCE F ILE E NCODING Plain Text files are expected to use UTF-8 encoding. 3.3 S TRING Q UOTES Always use double-quoted string format. However, when a string contains double quote characters, use a single-quote to avoid backslashes in the string. This improves readability. 3.4 W HITESPACE IN E XPRESSIONS & S TATEMENTS Avoid extra or missing whitespace in the following: Whitespace in Conditional expressions Assigning a keyword return value to a variable Proprietary and Confidential to NGA Human Resources Page 5 Robot Framework(RF) Coding Style Guide Add single space between Test data Table and asterisks *** *** *** *** Settings *** Variables *** Test Cases *** Keywords *** 3.5 C OMMENTS Block comments generally apply to some (or all) code that follows them, and are indented to the same level as that code. Each line of a block comment starts with a # and a single space (PEP-8). Avoid adding chain of #s to indicate start & end of a block comment. Also, do not use # followed by a series of dash to start/end a block comment. This will make the code look cluttered. Don’ts: Proprietary and Confidential to NGA Human Resources Page 6 Robot Framework(RF) Coding Style Guide 4 TEST SUITE STRUCTURE As discussed in Section 2.2, Test Suite Folder & Filename should be consistent with ALM. There should be a 1:1 correspondence between the Test Instances in ALM & the Test Scripts in GitLab. Hence, there should only be one Test Case per Test Suite (File). 4.1 T EST D ATA T ABLE N AMES The Test Data Table should follow the convention: *** + single whitespace + + single whitespace + *** Where: Table name = Settings, Variables, Test Cases, Keywords *** *** *** *** Settings *** Variables *** Test Cases *** Keywords *** The test scripter should follow the name, capitalization, spacing of the test data tables. 4.2 S ETTINGS T ABLE You should add the Documentation at the Settings table. The description can be picked-up from the test description in ALM. Resource setting should only source the required Resource file. Other Test Libraries & Resource Files needed can be sourced in the required Resource file. The master Resource file should only include common/generic resource files used in the test suite. Add Suite Teardown > Close Browser to ensure that the current browser will be closed so as not to affect the succeeding tests e.g. Auto-logoff due to inactivity Proprietary and Confidential to NGA Human Resources Page 7 Robot Framework(RF) Coding Style Guide 4.3 P RE - CONDITION AND P OST -C ONDITION Add Preconditions in "Suite Setup" & Cleanup in "Suite Teardown”. This will ensure that it will be triggered regardless if the test passed or failed. Use "Setup" as Setup keyword & "Cleanup" for Teardown. These keywords are defined in the resource file. Related Test cases using a common resource file should have same Setup & Teardown steps. In case of a different set of tests using the same flow but with different setup/teardown steps, you may use keyword Setup_ , Cleanup_ . *** Settings *** Suite Setup Setup Suite Teardown Cleanup Proprietary and Confidential to NGA Human Resources Page 8 Robot Framework(RF) Coding Style Guide 5 TEST CASE STRUCTURE The TestCase name should be the same as the Test Suitename –i.e. Filename. 5.1 T AGS Use [Tags] in your test case. The tags can be used to select tests for execution & can be set in Jenkins. [Tags] [Tags] [Tags] [Tags] [Tags] [Tags] [Tags] [Tags] automatedby: status: priority: testtype:sanity/regression/browser_compatibility system/client: module: submodule: functionality: 5.2 A BSTRACTION L EVEL Use appropriate Abstraction Level. Note that, the Test Case may be reviewed by non-technical person not familiar with the Robot Framework. Ideally, the script implementation can easily be traced from the Detailed Scenario. This means higher abstraction is expected for the Test Case. The lower abstraction can be implemented in the Keywords or Test Library. Don’t use too low abstraction level in your test case Don’t rely on comments instead of abstraction Sample Implementation: (To be discussed) Proprietary and Confidential to NGA Human Resources Page 9 Robot Framework(RF) Coding Style Guide 5.3 T EST C ASE S TRUCTURE Generally has these phases: o Preconditions o Action o Verification o Cleanup Keyword should describe what a test does o Use clear keywords with the appropriate abstraction level o Should contain enough information to run manually No complex logic o No for loops or if/else constructs o Test Cases should not look like scripts Maximum of 15 steps, preferably less Be consistent in the Abstraction Level used. Keep in mind, Customer/Product owner may have to review your test implementation Do not test without checks – so basically just the flow was verified Avoid Tests with unrelated checks. This is redundant & eats up execution time. Avoid dependencies between tests. The test suite should be standalone. The preconditions & cleanup are implemented within the test script. Use teardowns for cleanup Proprietary and Confidential to NGA Human Resources Page 10 Robot Framework(RF) Coding Style Guide 6 USER KEYWORDS 6.1 G ENERAL Name should be descriptive of the function & can easily be mapped in the Test Script Implementation plan All variables used in the keyword including the arguments, return & local variable in the implementation should be in lower case. It may contain programming logic (If/else, for loops) 6.2 C OMMENTS Add comments in your keyword to describe the action/flow if needed. Always keep in mind that others will be maintaining your code, so include in your comments your assumptions, default values, etc. 6.3 U SING K EYWORDS When using keywords in your Test Case or Keywords table, Robot framework may allow you to use mixed cases – that is, “Close Browser” or ”close Browser” are accepted. Use the case as it is written in the keyword definition. If you are using eclipse, the intelliSense will do it for you. Use [Arguments] , [Return] (Follow the case) Add explicit waits & Capture page screenshot if needed in the keywords & not in the test cases to hide the technical implementation. Proprietary and Confidential to NGA Human Resources Page 11 Robot Framework(RF) Coding Style Guide 7 VARIABLES Variables are used to encapsulate long and/or complicated values, and pass values from the command line or keywords. 7.1 N AMING Clear but not too long. Do not use generic variable name e.g. ${a}. Follow CamelCase Format. If name is long, you may use an underscore to improve readability. Do not use space as separator. e.g ${FirstName} Use case consistently: o Upper Case for Global Variable e.g. ${BROWSER} o CamelCase for Suite Variable (Declared in Variables table) e.g. ${JobType} o Lower Case for local variable e.g. return values of keywords assign to a variable. e.g. ${tmp} 7.2 A SSIGNING V ARIABLE VALUE When declaring a Variable in the Variables Table of the Test Suite, omit the equal sign(=). When assigning the return value of keyword to a variable, use the equal sign(=). Proprietary and Confidential to NGA Human Resources Page 12 Robot Framework(RF) Coding Style Guide 8 REFERENCES # REFERENCE 1 NGAHR_Test_Script_Implementation_Plan_Template. dotx 2 Writing Stable & maintainable RF Test Scripts 3 Automation PostReg Test Script Cleanup 4 Robot Framework – How to write Good TestCases 5 PEP 8 – Style Guide for Python 6 writing_maintainable_automated_acceptance_tests.pdf 7 Robot Framework – Quick Start Guide 8 Robot Framework Do’s & Don’ts Proprietary and Confidential to NGA Human Resources LOCATION https://drive.google.com/drive/#folders/0B6qCtvTgLEaQXFuMTloUUQ2T3M/0B6qCtvTgLEaVnRMeDBPSUtzcjQ/0B6qCtvTgLEafmRPbjJscjQ3NmlZY0YzNzRwQVNVV1NQZ0 1tcTJobWtqblFxVGZZZFVFMjA https://drive.google.com/drive/#folders/0B6qCtvTgLEaX1VLdWJnM1dCSFE/0B6qCtvTgLEaM2tndnFPVGpWZ1U/0B6qCtvTgLEaLXI1NzVpRl9xUmM https://docs.google.com/a/ngahr.com/presentation/d/1pjQr T43Q8DWY0nw12GRTkHHOsfout5Sxt7Bcv90FKlA/edit#sli de=id.p25 https://code.google.com/p/robotframework/wiki/HowToWrit eGoodTestCases#Passing_and_returning_values https://www.python.org/dev/peps/pep-0008/ http://dhemery.com/pdf/writing_maintainable_automated_a cceptance_tests.pdf http://robotframework.googlecode.com/hg/doc/userguide/R obotFrameworkUserGuide.html?r=2.8.5 http://www.slideshare.net/pekkaklarck/robot-frameworkdos-and-donts Page 13 Robot Framework(RF) Coding Style Guide 9 APPENDIX 9.1 D EFINITIONS # NAME Proprietary and Confidential to NGA Human Resources DESCRIPTION Page 14 NGA Human Resources is a global leader in helping organizations transform their business-critical HR operations to deliver more effective and efficient people-critical services. We help our clients become better employers through smarter, more streamlined business processes — to save money, manage employee life cycles, and support globally connected, agile organizations. This is how NGA makes HR work. What sets us apart is The NGA Advantage. It’s a combination of deep HR experience and insight, advanced technology platforms and applications and a global portfolio of flexible service delivery options. www.ngahr.com
Source Exif Data:
File Type : PDF File Type Extension : pdf MIME Type : application/pdf PDF Version : 1.5 Linearized : No Page Count : 16 Language : en-US Tagged PDF : Yes Author : admin Creator : Microsoft® Office Word 2007 Create Date : 2015:04:28 22:49:08+08:00 Modify Date : 2015:04:28 22:49:08+08:00 Producer : Microsoft® Office Word 2007EXIF Metadata provided by EXIF.tools