Testing — Why bother? Professor Larry Heimann Carnegie Mellon University Information Systems Program
Why do testing?
Comic of the Day...
Class example: romanic_testing Get the code at https://github.com/profh/romanic_testing
Unit testing assertions • assert(actual, message) # Asserts truth • assert_equal(expected, actual, message) • assert_in_delta(expected_float, actual_float, delta, message) • assert_match(pattern, string, message) • assert_nil(object, message) / assert_not_nil • assert_raise(Exception, ..., message) { block ... } • assert_di ff erence(expressions, di ff erence = 1, &block)
ActiveSupport::TestCase • By default, Rails uses ActiveSupport::TestCase to handle unit testing. • Rails ships with three types of tests: unit (models), functional (controllers), and integration (full MVC stack). • Every method in the test case with a name that starts with test_ represents a single test that gets executed by the framework • Before every test method the setup method is invoked, and afterwards the teardown method • Every test method makes one or more assertions about the behavior of the class under test
More on unit testing • Unit testing is done on models only • All models have a default test created upon rails generate model or rails generate sca ff old • Unit tests are stored in the directory test/models within a Rails project • Tests can be executed using rake test:units or rake test or by executing a particular file (e.g., ruby test/unit/owner_test.rb ) • Can measure test coverage with gems like simple_cov (what we will use in grading phases). These coverage tools are not perfect and especially vulnerable to false positive readings (i.e., says you have full coverage when you do not). No substitute for your own testing analysis.
Looking at basic unit tests in PATS Get the code at https://github.com/profh/PATS_67272
pItlh
Recommend
More recommend