Skip to content

Commit 7374545

Browse files
authored
Add documentation for the NO_DB_MIGRATION env var (#4800)
- Existing environment variable that was not documented - Useful for speeding up test load time in most situations - Minor refactor of implementation
1 parent 9b76208 commit 7374545

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

spec/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,54 @@ Used to test integration with [NATs](https://github.com/cloudfoundry/nats-releas
6666
Previously used to both generate the v2 API docs and test the user facing JSON response for the v2 API. Instead, write a request spec.
6767
To view the docs locally, cd into the `docs/v2` folder, run `python -mSimpleHTTPServer`, and navigate to `http://localhost:8000`.
6868

69+
## Speeding Up Tests
70+
71+
### Skip Database Recreate
72+
73+
By default, running specs with `spec_helper` will automatically:
74+
1. Drop all database tables and views
75+
2. Run all migrations
76+
3. Confirm that all migrations have run
77+
78+
See `spec_bootstrap.rb` for details.
79+
80+
This is an expensive operation and can increase the test load time
81+
significantly. Furthermore, it is typically not needed, since the database
82+
schema changes infrequently and there is other tooling in place to isolate data
83+
between tests (see `database_isolation.rb`).
84+
85+
To skip this step, set the `NO_DB_MIGRATION` environment variable. You will
86+
need to unset the variable, if you modify the database schema (i.e. adding a
87+
new migration).
88+
89+
Example performance improvement:
90+
91+
```sh
92+
❯ multitime -n 10 bundle exec rspec spec/unit/actions/app_create_spec.rb
93+
94+
...
95+
96+
===> multitime results
97+
1: bundle exec rspec spec/unit/actions/app_create_spec.rb
98+
Mean Std.Dev. Min Median Max
99+
real 23.121 2.199 20.605 22.416 28.194
100+
user 4.100 0.105 3.902 4.103 4.292
101+
sys 2.382 0.084 2.266 2.381 2.557
102+
```
103+
104+
```sh
105+
❯ NO_DB_MIGRATION=1 multitime -n 10 bundle exec rspec spec/unit/actions/app_create_spec.rb
106+
107+
...
108+
109+
===> multitime results
110+
1: bundle exec rspec spec/unit/actions/app_create_spec.rb
111+
Mean Std.Dev. Min Median Max
112+
real 9.659 0.202 9.408 9.601 9.997
113+
user 2.968 0.046 2.856 2.991 3.015
114+
sys 1.636 0.042 1.569 1.640 1.720
115+
```
116+
69117
## Running Tests In Preloaded (Fast) Mode:
70118

71119
Running unit tests is a good thing, but it can be annoying waiting for

spec/spec_helper.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,8 @@
8080
end
8181

8282
each_run_block = proc do
83-
# Moving this line into the init-block means that changes in code files aren't detected.
84-
if ENV['NO_DB_MIGRATION']
85-
VCAP::CloudController::SpecBootstrap.init(do_schema_migration: false)
86-
else
87-
VCAP::CloudController::SpecBootstrap.init(do_schema_migration: true)
88-
end
83+
# Moving SpecBootstrap.init into the init-block means that changes in code files aren't detected.
84+
VCAP::CloudController::SpecBootstrap.init(do_schema_migration: !ENV['NO_DB_MIGRATION'])
8985

9086
Dir[File.expand_path('support/**/*.rb', File.dirname(__FILE__))].each { |file| require file }
9187

0 commit comments

Comments
 (0)