diff options
author | Robert Speicher <robert@gitlab.com> | 2017-08-14 18:26:53 +0000 |
---|---|---|
committer | Robert Speicher <robert@gitlab.com> | 2017-08-14 18:26:53 +0000 |
commit | bd36637bdb45efebf2f1eb1119510a5708d83452 (patch) | |
tree | 73710c9ccf1081975a45e3ad867f3f78f5b32ccb /doc/development | |
parent | b5bc0fca068af0268d6bfb0deb8a72f37be76b6c (diff) | |
parent | caa498fd315fde3d4189da0b0293961dbcfa5e92 (diff) | |
download | gitlab-ce-bd36637bdb45efebf2f1eb1119510a5708d83452.tar.gz |
Merge branch '35804-document-table-based-tests' into 'master'
Use rspec-parameterized for table-based tests
Closes #35804
See merge request !13469
Diffstat (limited to 'doc/development')
-rw-r--r-- | doc/development/testing.md | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/doc/development/testing.md b/doc/development/testing.md index c7eac3cf40c..efd56484b12 100644 --- a/doc/development/testing.md +++ b/doc/development/testing.md @@ -279,6 +279,43 @@ end - Avoid scenario titles that add no information, such as "successfully". - Avoid scenario titles that repeat the feature title. +### Table-based / Parameterized tests + +This style of testing is used to exercise one piece of code with a comprehensive +range of inputs. By specifying the test case once, alongside a table of inputs +and the expected output for each, your tests can be made easier to read and more +compact. + +We use the [rspec-parameterized](https://github.com/tomykaira/rspec-parameterized) +gem. A short example, using the table syntax and checking Ruby equality for a +range of inputs, might look like this: + +```ruby +describe "#==" do + using Rspec::Parameterized::TableSyntax + + let(:project1) { create(:project) } + let(:project2) { create(:project) } + where(:a, :b, :result) do + 1 | 1 | true + 1 | 2 | false + true | true | true + true | false | false + project1 | project1 | true + project2 | project2 | true + project 1 | project2 | false + end + + with_them do + it { expect(a == b).to eq(result) } + + it 'is isomorphic' do + expect(b == a).to eq(result) + end + end +end +``` + ### Matchers Custom matchers should be created to clarify the intent and/or hide the |