Learning Objectives
Function in Programming
Test Execution Side Effects
Add dynamic to test data, using random number, timestamp
Use of Faker test data generation library
Knowledge Point: Function
A function is a sequence of program instructions that performs a specific task.
Let’s do a simple exercise. Create a text file with name minus.rb with the content below:
# definition of a function
def minus(a, b)
a - b
end
# use of the function
minus(10, 2)
To run it from the command line:
> ruby minus.rb
There is no output, as we didn’t print it out. Change the last line.
puts minus(10, 2)
Run the program (minus.rb) again:
> ruby minus.rb
8
The output is not clear. We could add some text to make the output “The result is: 8”.
The code below will return an error.
puts "The result is: " + minus(10, 2)
The error is:
minus.rb:5:in `+': no implicit conversion of Integer into String (TypeError)
puts "The result is: " + minus(10, 2)
^^^^^^^^^^^^
from minus.rb:5:in `<main>'
This is because adding an integer to a String is not allowed. A simple solution:
puts "The result is: #{ minus(10, 2) }"
You can understand this way: `#{ code }
` returns a string.
Knowledge Point: Test Execution Side Effects
We introduced the concept in the last episode (#23). Let’s explore this further with the example of CRUD tests. For example, adding a professional test (with the same name) is wrong for test automation: the second test execution onwards would fail.
Keep reading with a 7-day free trial
Subscribe to The Agile Way to keep reading this post and get 7 days of free access to the full post archives.