-
testing is a part of ruby best practice
-
manual testing does not scale
-
TDD - Test Driven Development
-
Unit Tests
-
Integration Tests
-
Test Coverage
- test for behaviour, 100% coverage is not required
-
where to write tests
We have been writing tests with the classes we have written.
city_count = subcountry_city_count(cities, "Australia", "Victoria")
print "ERROR!!! " if city_count != 90
puts("Expect Victoria, Australia to have 90 cities and got #{city_count}")
city_count = subcountry_city_count(cities, "United States", "Florida")
print "ERROR!!! " if city_count != 227
puts("Expect Florida, United States to have 227 and got #{city_count}")
It is a convention to write tests in separate files so the can be run independantly of the applicaiton code.
Automated testing options
- rspec
- minitest
- the default in Rails
- part of xUnit test family eg: jUnit, nUnit etc
- uses conventions
- files are named after the class under test ie:
class_name_test.rb
- tests are named after the method they are testing
test_method_name
- Rails scripts will run all files that end in
_test.rb
- add the require for the monitest gem at the top of the test file
require 'minitest/autorun'
- dd the require for the class under test
- files are named after the class under test ie:
Without Rails minitest needs to be installed
gem install minitest
New ruby syntax: Inheritance
- Inheritance a common pattern when using frameworks
- we will use it a lot when using Rails
require 'minitest/autorun'
require_relative 'product'
class ProductTest < MiniTest::Test
def setup
@product = Product.new({
:name => 'Widgit',
:price => '10.99'
})
end
def test_name
assert_equal 'Widgit', @product.name
end
def test_price
assert_equal 10.99, @product.price
end
end
product_test.rb
- implement the
Product
class
- implement the
aussie_date_test.rb
- instructions in the file
- class that has Australian formats for dates. This class would provide a central place in an application for date formatting. This would consistant formatting is used throughout an application
string_validator_test.rb
- instructions in the file
- class that contains regex validation. Another centralised utility class that would help ensure consistancy.
- refactor your Person class and tests from yesterday
- create a People class that contains an array of Person objects and all the methods that work on the array
- review the design with a neighbour and / or teacher
- create a minitest PeopleTest class and add tests using the test code you wrote
- create a minitest PersonTest class and add tests