Functional Test Refactoring: Move to Helper
Make a helper function available to all test scripts
This is one of the 6 Functional Test Refactorings (see the introduction here):
Move Function to Helper
Extract Page Class
Introduce Page Object
Rename
The structure of this article is the same as “Extract Function” (see details here). The same test project that helps you do refactoring exercises quickly:
> cd my-working-dir
> git clone https://github.com/testwisely/agiletravel-ui-tests
The test project is at my-working-dir/agiletravel-ui-tests/pre-refactoring
.
Motivation
You use the same functions defined in separate test script files.
As a common practice, all test scripts in a project share a common helper. By moving a function to the helper, it is made available to all test cases.
Sample Tests (before)
A login function created by Tester#1 in TestScript#10
def login(user, pass)
# ...
end
Another login function created by Tester#2 in TestScript#20
def login(user_name, password)
# ...
end
Issues with the tests
Violates DRY (Don’t Repeat Yourself)
There are several cases of the same function (which might be named differently) that do the same thing.
Action
Move the function to the shared test helper, delete similar ones and use the one in the test helper.
Prerequisite
The test project follows the Maintainable Automated Test Design, and has a shared test helper.
Mechanics
Identify a function
Move to the helper
Rerun the test
Refactoring Steps in TestWise
Choose the function (by moving the caret to its definition)
Invoke refactoring (and preview)
Confirm to apply the refactoring
Expected Result
The function definition is now moved to the shared test helper file
After a function is moved to the helper, you may use it in other tests.
Confirmation Dialog
Sample Tests (after)
The login
functions in TestScript#10 and TestScript#20 are deleted. A new function is added to the test helper.
# test_helperdef login(user_name, password)
# ...
end
Demonstration (animated GIF)
The test script file in the sample project: spec/login_spec.rb
Move a function (from a test) to the test helper
Firstly, move the caret to the function name, then apply this refactoring.
2. Use an already-defined function in the test helper
3. You can quickly navigate to the definition of a function from tests, and press “Ctrl+B” (or Cmd +B on macOS) on the function name.
Demonstration (Video)
Move functions to the shared test helper
Use the functions in the shared test helper
Navigate to the function definition from tests.
Benefits
Reusable
A function in the shared test helper is usable in all test scriptsDRY (Don’t Repeat Yourself)
The definition of a test operation (e.g. login, logout) only exists in one place, i.e. the shared test helper.Auto-completable
The functions in the test helper are available to all test scripts. In a functional testing IDE, these reusable functions may be auto-completable, like below.
This will not only increase efficiency but also reduce duplications (other testers might create similar methods).
Q & A
How to make the functions defined in
test_helper.rb
automatically available to all test scripts?All test scripts (based on the test template) load the test helper, at the first line.
load File.dirname(__FILE__) + '/../test_helper.rb'
describe "Passenger" do
include TestHelper
# ...
end
Exercises
Prerequisite: two functions login
and logoff
exist in login_spec.rb
, created by Extract to Function refactoring.
Move
login
function inlogin_spec.rb
to the helper, via the Refactoring menu itemMove
logoff
function inlogin_spec.rb
, via keyboard shortcutInspect these two functions in
test_helper.rb
Use
login
function in another test script file, using auto-complete: typelo
then press the Ctrl+Space key.Navigate to the definition of a function quickly. Move the caret of the function name in a test script, then press Ctrl + B key.
Related Refactorings