You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

background.feature 15 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  1. Feature: Background
  2. Often you find that several scenarios in the same feature start with
  3. a common context.
  4. Cucumber provides a mechanism for this, by providing a `Background` keyword
  5. where you can specify steps that should be run before each scenario in the
  6. feature. Typically these will be `Given` steps, but you can use any steps
  7. that you need to.
  8. **Hint:** if you find that some of the scenarios don't fit the background,
  9. consider splitting them into a separate feature.
  10. Background:
  11. Given a file named "features/passing_background.feature" with:
  12. """
  13. Feature: Passing background sample
  14. Background:
  15. Given '10' cukes
  16. Scenario: passing background
  17. Then I should have '10' cukes
  18. Scenario: another passing background
  19. Then I should have '10' cukes
  20. """
  21. And a file named "features/scenario_outline_passing_background.feature" with:
  22. """
  23. Feature: Passing background with scenario outlines sample
  24. Background:
  25. Given '10' cukes
  26. Scenario Outline: passing background
  27. Then I should have '<count>' cukes
  28. Examples:
  29. | count |
  30. | 10 |
  31. Scenario Outline: another passing background
  32. Then I should have '<count>' cukes
  33. Examples:
  34. | count |
  35. | 10 |
  36. """
  37. And a file named "features/background_tagged_before_on_outline.feature" with:
  38. """
  39. @background_tagged_before_on_outline
  40. Feature: Background tagged Before on Outline
  41. Background:
  42. Given this step passes
  43. Scenario Outline: passing background
  44. Then I should have '<count>' cukes
  45. Examples:
  46. | count |
  47. | 888 |
  48. """
  49. And a file named "features/failing_background.feature" with:
  50. """
  51. Feature: Failing background sample
  52. Background:
  53. Given this step raises an error
  54. And '10' cukes
  55. Scenario: failing background
  56. Then I should have '10' cukes
  57. Scenario: another failing background
  58. Then I should have '10' cukes
  59. """
  60. And a file named "features/scenario_outline_failing_background.feature" with:
  61. """
  62. Feature: Failing background with scenario outlines sample
  63. Background:
  64. Given this step raises an error
  65. Scenario Outline: failing background
  66. Then I should have '<count>' cukes
  67. Examples:
  68. | count |
  69. | 10 |
  70. Scenario Outline: another failing background
  71. Then I should have '<count>' cukes
  72. Examples:
  73. | count |
  74. | 10 |
  75. """
  76. And a file named "features/pending_background.feature" with:
  77. """
  78. Feature: Pending background sample
  79. Background:
  80. Given this step is pending
  81. Scenario: pending background
  82. Then I should have '10' cukes
  83. Scenario: another pending background
  84. Then I should have '10' cukes
  85. """
  86. And a file named "features/failing_background_after_success.feature" with:
  87. """
  88. Feature: Failing background after previously successful background sample
  89. Background:
  90. Given this step passes
  91. And '10' global cukes
  92. Scenario: passing background
  93. Then I should have '10' global cukes
  94. Scenario: failing background
  95. Then I should have '10' global cukes
  96. """
  97. And a file named "features/failing_background_after_success_outline.feature" with:
  98. """
  99. Feature: Failing background after previously successful background sample
  100. Background:
  101. Given this step passes
  102. And '10' global cukes
  103. Scenario Outline: passing background
  104. Then I should have '<count>' global cukes
  105. Examples:
  106. | count |
  107. | 10 |
  108. Scenario Outline: failing background
  109. Then I should have '<count>' global cukes
  110. Examples:
  111. | count |
  112. | 10 |
  113. """
  114. And a file named "features/multiline_args_background.feature" with:
  115. """
  116. Feature: Passing background with multiline args
  117. Background:
  118. Given table
  119. | a | b |
  120. | c | d |
  121. And multiline string
  122. \"\"\"
  123. I'm a cucumber and I'm okay.
  124. I sleep all night and I test all day
  125. \"\"\"
  126. Scenario: passing background
  127. Then the table should be
  128. | a | b |
  129. | c | d |
  130. Then the multiline string should be
  131. \"\"\"
  132. I'm a cucumber and I'm okay.
  133. I sleep all night and I test all day
  134. \"\"\"
  135. Scenario: another passing background
  136. Then the table should be
  137. | a | b |
  138. | c | d |
  139. Then the multiline string should be
  140. \"\"\"
  141. I'm a cucumber and I'm okay.
  142. I sleep all night and I test all day
  143. \"\"\"
  144. """
  145. And the standard step definitions
  146. And a file named "features/step_definitions/cuke_steps.rb" with:
  147. """
  148. Given /^'(.+)' cukes$/ do |cukes| x=1
  149. raise "We already have #{@cukes} cukes!" if @cukes
  150. @cukes = cukes
  151. end
  152. Given /^'(.+)' global cukes$/ do |cukes| x=1
  153. $scenario_runs ||= 0
  154. raise 'FAIL' if $scenario_runs >= 1
  155. $cukes = cukes
  156. $scenario_runs += 1
  157. end
  158. Then /^I should have '(.+)' global cukes$/ do |cukes| x=1
  159. expect($cukes).to eq cukes
  160. end
  161. Then /^I should have '(.+)' cukes$/ do |cukes| x=1
  162. expect(@cukes).to eq cukes
  163. end
  164. Before('@background_tagged_before_on_outline') do
  165. @cukes = '888'
  166. end
  167. After('@background_tagged_before_on_outline') do
  168. expect(@cukes).to eq '888'
  169. end
  170. """
  171. Scenario: run a specific scenario with a background
  172. When I run `cucumber -q features/passing_background.feature:9`
  173. Then it should pass with exactly:
  174. """
  175. Feature: Passing background sample
  176. Background:
  177. Given '10' cukes
  178. Scenario: another passing background
  179. Then I should have '10' cukes
  180. 1 scenario (1 passed)
  181. 2 steps (2 passed)
  182. """
  183. Scenario: run a feature with a background that passes
  184. When I run `cucumber -q features/passing_background.feature`
  185. Then it should pass with exactly:
  186. """
  187. Feature: Passing background sample
  188. Background:
  189. Given '10' cukes
  190. Scenario: passing background
  191. Then I should have '10' cukes
  192. Scenario: another passing background
  193. Then I should have '10' cukes
  194. 2 scenarios (2 passed)
  195. 4 steps (4 passed)
  196. """
  197. Scenario: run a feature with scenario outlines that has a background that passes
  198. When I run `cucumber -q features/scenario_outline_passing_background.feature`
  199. Then it should pass with exactly:
  200. """
  201. Feature: Passing background with scenario outlines sample
  202. Background:
  203. Given '10' cukes
  204. Scenario Outline: passing background
  205. Then I should have '<count>' cukes
  206. Examples:
  207. | count |
  208. | 10 |
  209. Scenario Outline: another passing background
  210. Then I should have '<count>' cukes
  211. Examples:
  212. | count |
  213. | 10 |
  214. 2 scenarios (2 passed)
  215. 4 steps (4 passed)
  216. """
  217. Scenario: run a feature with scenario outlines that has a background that passes
  218. When I run `cucumber -q features/background_tagged_before_on_outline.feature`
  219. Then it should pass with exactly:
  220. """
  221. @background_tagged_before_on_outline
  222. Feature: Background tagged Before on Outline
  223. Background:
  224. Given this step passes
  225. Scenario Outline: passing background
  226. Then I should have '<count>' cukes
  227. Examples:
  228. | count |
  229. | 888 |
  230. 1 scenario (1 passed)
  231. 2 steps (2 passed)
  232. """
  233. Scenario: run a feature with a background that fails
  234. When I run `cucumber -q features/failing_background.feature`
  235. Then it should fail with exactly:
  236. """
  237. Feature: Failing background sample
  238. Background:
  239. Given this step raises an error
  240. error (RuntimeError)
  241. ./features/step_definitions/steps.rb:2:in `/^this step raises an error$/'
  242. features/failing_background.feature:4:in `this step raises an error'
  243. And '10' cukes
  244. Scenario: failing background
  245. Then I should have '10' cukes
  246. Scenario: another failing background
  247. Then I should have '10' cukes
  248. Failing Scenarios:
  249. cucumber features/failing_background.feature:7
  250. cucumber features/failing_background.feature:10
  251. 2 scenarios (2 failed)
  252. 6 steps (2 failed, 4 skipped)
  253. """
  254. Scenario: run a feature with scenario outlines that has a background that fails
  255. When I run `cucumber -q features/scenario_outline_failing_background.feature`
  256. Then it should fail with exactly:
  257. """
  258. Feature: Failing background with scenario outlines sample
  259. Background:
  260. Given this step raises an error
  261. error (RuntimeError)
  262. ./features/step_definitions/steps.rb:2:in `/^this step raises an error$/'
  263. features/scenario_outline_failing_background.feature:4:in `this step raises an error'
  264. Scenario Outline: failing background
  265. Then I should have '<count>' cukes
  266. Examples:
  267. | count |
  268. | 10 |
  269. Scenario Outline: another failing background
  270. Then I should have '<count>' cukes
  271. Examples:
  272. | count |
  273. | 10 |
  274. Failing Scenarios:
  275. cucumber features/scenario_outline_failing_background.feature:10
  276. cucumber features/scenario_outline_failing_background.feature:16
  277. 2 scenarios (2 failed)
  278. 4 steps (2 failed, 2 skipped)
  279. """
  280. Scenario: run a feature with a background that is pending
  281. When I run `cucumber -q features/pending_background.feature`
  282. Then it should pass with exactly:
  283. """
  284. Feature: Pending background sample
  285. Background:
  286. Given this step is pending
  287. Scenario: pending background
  288. Then I should have '10' cukes
  289. Scenario: another pending background
  290. Then I should have '10' cukes
  291. 2 scenarios (2 pending)
  292. 4 steps (2 skipped, 2 pending)
  293. """
  294. Scenario: background passes with first scenario but fails with second
  295. When I run `cucumber -q features/failing_background_after_success.feature`
  296. Then it should fail with exactly:
  297. """
  298. Feature: Failing background after previously successful background sample
  299. Background:
  300. Given this step passes
  301. And '10' global cukes
  302. Scenario: passing background
  303. Then I should have '10' global cukes
  304. Scenario: failing background
  305. And '10' global cukes
  306. FAIL (RuntimeError)
  307. ./features/step_definitions/cuke_steps.rb:8:in `/^'(.+)' global cukes$/'
  308. features/failing_background_after_success.feature:5:in `'10' global cukes'
  309. Then I should have '10' global cukes
  310. Failing Scenarios:
  311. cucumber features/failing_background_after_success.feature:10
  312. 2 scenarios (1 failed, 1 passed)
  313. 6 steps (1 failed, 1 skipped, 4 passed)
  314. """
  315. @global_state
  316. Scenario: background passes with first outline scenario but fails with second
  317. When I run `cucumber -q features/failing_background_after_success_outline.feature`
  318. Then it should fail with exactly:
  319. """
  320. Feature: Failing background after previously successful background sample
  321. Background:
  322. Given this step passes
  323. And '10' global cukes
  324. Scenario Outline: passing background
  325. Then I should have '<count>' global cukes
  326. Examples:
  327. | count |
  328. | 10 |
  329. Scenario Outline: failing background
  330. Then I should have '<count>' global cukes
  331. Examples:
  332. | count |
  333. | 10 |
  334. FAIL (RuntimeError)
  335. ./features/step_definitions/cuke_steps.rb:8:in `/^'(.+)' global cukes$/'
  336. features/failing_background_after_success_outline.feature:5:in `'10' global cukes'
  337. Failing Scenarios:
  338. cucumber features/failing_background_after_success_outline.feature:19
  339. 2 scenarios (1 failed, 1 passed)
  340. 6 steps (1 failed, 1 skipped, 4 passed)
  341. """
  342. @global_state
  343. Scenario: background passes with first outline scenario but fails with second (--expand)
  344. When I run `cucumber -x -q features/failing_background_after_success_outline.feature`
  345. Then it should fail with exactly:
  346. """
  347. Feature: Failing background after previously successful background sample
  348. Background:
  349. Given this step passes
  350. And '10' global cukes
  351. Scenario Outline: passing background
  352. Then I should have '<count>' global cukes
  353. Examples:
  354. Example: | 10 |
  355. Then I should have '10' global cukes
  356. Scenario Outline: failing background
  357. Then I should have '<count>' global cukes
  358. Examples:
  359. Example: | 10 |
  360. And '10' global cukes
  361. FAIL (RuntimeError)
  362. ./features/step_definitions/cuke_steps.rb:8:in `/^'(.+)' global cukes$/'
  363. features/failing_background_after_success_outline.feature:5:in `'10' global cukes'
  364. Then I should have '10' global cukes
  365. Failing Scenarios:
  366. cucumber features/failing_background_after_success_outline.feature:19
  367. 2 scenarios (1 failed, 1 passed)
  368. 6 steps (1 failed, 1 skipped, 4 passed)
  369. """
  370. Scenario: background with multline args
  371. Given a file named "features/step_definitions/steps.rb" with:
  372. """
  373. Given /^table$/ do |table| x=1
  374. @table = table
  375. end
  376. Given /^multiline string$/ do |string| x=1
  377. @multiline = string
  378. end
  379. Then /^the table should be$/ do |table| x=1
  380. expect(@table.raw).to eq table.raw
  381. end
  382. Then /^the multiline string should be$/ do |string| x=1
  383. expect(@multiline).to eq string
  384. end
  385. """
  386. When I run `cucumber -q features/multiline_args_background.feature`
  387. Then it should pass with exactly:
  388. """
  389. Feature: Passing background with multiline args
  390. Background:
  391. Given table
  392. | a | b |
  393. | c | d |
  394. And multiline string
  395. \"\"\"
  396. I'm a cucumber and I'm okay.
  397. I sleep all night and I test all day
  398. \"\"\"
  399. Scenario: passing background
  400. Then the table should be
  401. | a | b |
  402. | c | d |
  403. Then the multiline string should be
  404. \"\"\"
  405. I'm a cucumber and I'm okay.
  406. I sleep all night and I test all day
  407. \"\"\"
  408. Scenario: another passing background
  409. Then the table should be
  410. | a | b |
  411. | c | d |
  412. Then the multiline string should be
  413. \"\"\"
  414. I'm a cucumber and I'm okay.
  415. I sleep all night and I test all day
  416. \"\"\"
  417. 2 scenarios (2 passed)
  418. 8 steps (8 passed)
  419. """

No Description

Contributors (1)