Training testing
Noosfero follows a strict TDD style. This means you should create your tests before coding!
This involves an education process of the developer and also knowing the best practices
of testing, for each test layer. If you are not an experienced tester, please see first tutorials section.
Tutorials
- TechTV Video Series: Efficient Rails Test-Driven Development, Class 1 of 6 and slides at slideshare
- Ryan Bates: How I test
- Write automated tests for your code
Running
To run all tests, run
rake test. There are also others tasks, please see
rake -T
To run a set of tests, just run
ruby test/unit/article_test.rb,
ruby test/functionals/cms_controller_test.rb,
cucumber features/new_content_on_cms.feature or
cucumber -p selenium features/new_content_on_cms.feature
For unit and functionals you may use
-n /regex/ option to filter tests by name.
Setting up a clean environment.
Database may have some data when starting tests.
db/schema.rb may be dirty.
Config files may change behaviour. Uncommited changes can left forgotten code.
Take
care to backup all untracked files and to commit changes.
clean and
checkout
will destroy them.
rm .gitignore
git clean -df
git checkout .
cp gitignore.example .gitignore
cp config/database.yml.sqlite3 config/database.yml
cp config/solr.yml.dist config/solr.yml
mkdir tmp log
rake makemo
rake db:schema:load
rake db:migrate
Now you can run
rake test with everything clear
Layers
Testing is done in the MVC layers. As a general rule, models are always tested, but controllers
or views may not get automated tests, depending on the case.
Units
Unit tests are most important tests for your code. This ensure data is read and written from the
database the way defined by the business rules.
Data creation
You should choose the method to create your data with some considerations. The most common
is to share data into the
setup method
Factories
Noosfero has its own simple factory system. It generates required data with a
serial number for models, which you may replace or add new fields specific for your tests.
This factory is coded at
test/factories.rb and have many performance considerations,
providing different methods for different needs. Here comes a table comparing them:
| |
Goes to DB |
Insert using ActiveRecord |
Factory with defaults_for |
Performance |
Validate |
| fast_create |
+ |
- |
- |
+ |
- |
| create |
+ |
+ |
+ |
- |
+ |
| build |
- |
+ |
+ |
+/- |
- |
Fixtures
Fixtures are not a good way of providing test data, since they make the tests not self-contained (you have to look elsewhere to know what the test is supposed to test).
Fixtures use is
not recommended.
Functionals
Tests that the controller get the right data from the
Models and pass them
with the right variables to
Views.
Integration
Integration tests sometimes replaces functionals tests, as it test the output to user.
Plain views
Plain Cucumber tests will be enough and easy to ensure things are placed on the view and brings the desired user navigation.
Javascript and Ajax views
Cucumber with Selenium tests run a browser and simulates a user doing the programmed cucumber actions.
The Selenium, Cucumber and webrat versions used by Noosfero is still unstable with Selenium.
Plugins
Run
rake -T to see that Noosfero already created some units test tasks so that you make run all your tests at once.
Frameworks
Both
TestCase? and RSpec are supported by Noosfero.
Documentation at
- TestCase API reference
- RSpec API reference
For RSpec integration with
TestCase? see
here
References