Extend Beyond Unit Testing
In the 1th part of this two-part mini series, we learned how to promote unit testing with dynamic languages. This article will demonstrate the advantages of the integrated environment in functional testing and integration testing. Unit tests include testing small pieces of code, such as methods, and often isolating them from the surrounding elements. Functional testing and integration testing are testing more and more parts of the application. Functional testing is used to test a single feature (usually involving an interface), the business code that executes the task, and the code that interacts with the middleware service, such as a database. Integration testing is used to test multiple different features of an application. (Functional testing is often referred to as integration testing under less stringent conditions.) )
Java developers have achieved impressive results in resolving unit testing issues, but they have not brought much exciting news to the integration test. Most Java test frameworks, such as JUnit or TestNG, focus primarily on unit testing. One reason for the lack of an integrated testing framework in Java programming is the lack of a centralized architecture or development philosophy. In the following section, I will continue to use the Ruby on Rails example, which focuses on functional testing and the new Rails integration test framework. You will see that it is much easier to test when using the integrated test framework.
Run Tests
If you haven't read the 1th part, read it first. Then, if you want to follow this article to write code, make sure you've got a working Rails application. In the 1th part, a simple unit test and several fixture are implemented. If you write code along with part 1th, but you don't remember whether the application is working, you can take advantage of the test case, switch to the project directory, and then run Rake. Listing 1 shows my results:
Listing 1. Run all tests with rake
> Bruce-tates-computer:~/rails/trails batate$ rake
(in/users/batate/rails/trails)
/usr/local/ Ror/bin/ruby-ilib:test
"/usr/local/ror/lib/ruby/gems/1.8/gems/rake-0.7.0/lib/rake/rake_test_loader.rb"
" Test/functional/trails_controller_test.rb "
Loaded suite/usr/local/ror/lib/ruby/gems/1.8/gems/rake-0.7.0/lib/ Rake/rake_test_loader
started
Eeeeeeeeeeeeeeee
finished in 0.070797 seconds.
1) Error:
Test_create (trailscontrollertest):
Errno::enoent:no such file or directory-/tmp/mysql.sock
/US r/local/ror/lib/ruby/gems/1.8/gems/activerecord-1.14.0/
Lib/active_record/vendor/mysql.rb:104:in ' Initialize '
/usr/local/ror/lib/ruby/gems/1.8/gems/activerecord-1.14.0/
lib/active_record/vendor/mysql.rb : 104:in ' real_connect '
/usr/local/ror/lib/ruby/gems/1.8/gems/activerecord-1.14.0/
Lib/active_record/conne Ction_adapters/mysql_adapter.rb:331:in ' Connect '
... results deleted ...
8 Tests, 0 assertions, 0 failures errors
/usr/local/ror/bin/ruby-ilib:test/usr/local/ror/lib/ruby/gems/1.8/gems/ rake-0.7.0/
lib/rake/rake_test_loader.rb
Rake aborted!
Test Failures
(for the full trace by running task with--trace)
You can see that there are some problems: rake generated 16 errors. Track shows that Rails cannot establish a connection. I forgot to start the database engine. I will start the database engine and then run rake again. This time I got the result shown in Listing 2:
Listing 2. Pass the test within rake
rake
(in /Users/batate/rails/trails)
/usr/local/ror/bin/ruby -Ilib:test
"/usr/local/ror/lib/ruby/gems/1.8/gems/rake-0.7.0/lib/rake/rake_test_loader.rb"
"test/unit/trail_test.rb"
Loaded suite /usr/local/ror/lib/ruby/gems/1.8/gems/rake- 0.7.0/lib/rake/rake_test_loader
Started
...
Finished in 0.09541 seconds.
3 tests, 5 assertions, 0 failures, 0 errors
/usr/local/ror/bin/ruby -Ilib:test
"/usr/local/ror/lib/ruby/gems/1.8/gems/rake-0.7.0/lib/rake/rake_test_loader.rb"
"test/functional/trails_controller_test.rb"
Loaded suite /usr/local/ror/lib/ruby/gems/1.8/gems/rake- 0.7.0/lib/rake/rake_test_loader
Started
........
Finished in 0.169756 seconds.
8 tests, 28 assertions, 0 failures, 0 errors
That's much better. The tests work, and we're ready to build more test cases. If you look at listing 2 carefully, Rake generates two sets of results. The first group (unit tests in part 1th) should look familiar. The next group is a feature test that is automatically generated from the framework.