Behavior-Driven Development
      с RSpec и Cucumber
      Стефан Кънев
      http://skanev.com/
      27.06.2009, TechCam...
Автоматизирано тестване




Tuesday, April 27, 2010
Unit Tests


                    Test-Driven Development


        Behavior-Driven Development
Tuesday, April 27, 2010
1. Unit test-ове
                    2. Test-Driven
                    3. Behavior-Driven
                    4. RSpec
  ...
1. Unit test-ове
                    2. Test-Driven
                    3. Behavior-Driven
                    4. RSpec
  ...
class Programmer
      def implement_a_change(project, change)
        files = self.open_related_files(project, change)
  ...
Repetitive...




Tuesday, April 27, 2010
...boring...




Tuesday, April 27, 2010
...error-prone




Tuesday, April 27, 2010
Машина, която да го
                           прави вместо нас
Tuesday, April 27, 2010
Програма, която да го
                            прави вместо нас
Tuesday, April 27, 2010
Unit Test




Tuesday, April 27, 2010
Пишем код, който
                 проверява дали друг код
                    работи правилно.


Tuesday, April 27, 2010
С един бутон




                          Да                  Не
Tuesday, April 27, 2010
caladan:trane aquarius$ rake spec
        (in /work/trane)
        ..........................................................
caladan:trane aquarius$ rake spec
       (in /work/trane)
       ............................................................
I can has value?



Tuesday, April 27, 2010
Време

Tuesday, April 27, 2010
Regression testing




Tuesday, April 27, 2010
Tuesday, April 27, 2010
Tuesday, April 27, 2010
Tuesday, April 27, 2010
Tuesday, April 27, 2010
Документация
          (развален английски + стари версии = fun)

Tuesday, April 27, 2010
Спецификация




Tuesday, April 27, 2010
Tuesday, April 27, 2010
1. Unit test-ове
                    2. Test-Driven
                    3. Behavior-Driven
                    4. RSpec
  ...
Unit test-овете са
                   просто инструмент



Tuesday, April 27, 2010
Test-Driven Development е
          подход към писането на
          софтуер


Tuesday, April 27, 2010
test-first




Tuesday, April 27, 2010
red/green/refactor




Tuesday, April 27, 2010
red/green/refactor


          Възможно най-простия тест,
          който проверява функцио-
          налността и не мина...
red/green/refactor


          Най-простата имплементация,
          която кара тестът да минава
          успешно.


Tues...
red/green/refactor


          Отстраняване на повторения,
          докато кодът не стане
          достатъчно качествен....
Tuesday, April 27, 2010
малки стъпки




Tuesday, April 27, 2010
Test-Driven Design




Tuesday, April 27, 2010
Преизползваемост




Tuesday, April 27, 2010
Low coupling




Tuesday, April 27, 2010
Добър интерфейс




Tuesday, April 27, 2010
scope creep




Tuesday, April 27, 2010
up-front design




Tuesday, April 27, 2010
1. Unit test-ове
                    2. Test-Driven
                    3. Behavior-Driven
                    4. RSpec
  ...
Test-Driven Development
                          is not about testing



Tuesday, April 27, 2010
TDD ≠ QA



Tuesday, April 27, 2010
Опитните TDD-ри
             използват процеса за
           да открият поведението,
           което системата трябва
   ...
Behavior-Driven
                           Development


Tuesday, April 27, 2010
BDD ≈ TDD++



Tuesday, April 27, 2010
"Getting the words right"




Tuesday, April 27, 2010
Test-Driven         Behavior-Driven




Tuesday, April 27, 2010
Assertion   Expectation




Tuesday, April 27, 2010
Test Method        Code Example




Tuesday, April 27, 2010
Test Case       Example Group




Tuesday, April 27, 2010
Domain-Driven Design




Tuesday, April 27, 2010
Domain Driven
          Design
          Eric Evans




Tuesday, April 27, 2010
Близки до проблемната
                        област думи



Tuesday, April 27, 2010
Разбираем за
                          бизнеса език



Tuesday, April 27, 2010
BDD = TDD + DDD
                               (TLA FTW)




Tuesday, April 27, 2010
Tuesday, April 27, 2010
BDD = TDD + DDD




Tuesday, April 27, 2010
BDD = TDD + DDD
                BDD = (T + D)DD




Tuesday, April 27, 2010
BDD = TDD + DDD
                BDD = (T + D)DD
                B=T+D



Tuesday, April 27, 2010
BDD = TDD + DDD
                BDD = (T + D)DD
                B=T+D
                интензитет = време + доза

Tuesday, ...
1. Unit test-ове
                    2. Test-Driven
                    3. Behavior-Driven
                    4. RSpec
  ...
Друг "синтаксис"




Tuesday, April 27, 2010
Друг "синтаксис"


                xUnit     assert_equal 42, answer




Tuesday, April 27, 2010
Друг "синтаксис"


                xUnit     assert_equal 42, answer

                RSpec     answer.should equal(42)


...
Друг "синтаксис"


                xUnit     assert_equal 42, answer

                RSpec     answer.should equal(42)

 ...
Tuesday, April 27, 2010
assert_equal 42, result




Tuesday, April 27, 2010
assert_equal 42, result
                          result.should == 42




Tuesday, April 27, 2010
assert_equal 42, result
                          result.should == 42
    assert result.nil?




Tuesday, April 27, 2010
assert_equal 42, result
                          result.should == 42
    assert result.nil?
                          res...
assert_equal 42, result
                          result.should == 42
    assert result.nil?
                          res...
assert_equal 42, result
                          result.should == 42
    assert result.nil?
                          res...
assert_equal 42, result
                          result.should == 42
    assert result.nil?
                          res...
assert_equal 42, result
                          result.should == 42
    assert result.nil?
                          res...
assert_equal 42, result
                          result.should == 42
    assert result.nil?
                          res...
assert_equal 42, result
                          result.should == 42
    assert result.nil?
                          res...
Tuesday, April 27, 2010
assert_raise(InvalidRecord) do
                     user.save!
                   end




Tuesday, April 27, 2010
assert_raise(InvalidRecord) do
                     user.save!
                   end

                   lambda { user.sa...
assert_raise(InvalidRecord) do
                     user.save!
                   end

                   lambda { user.sa...
assert_raise(InvalidRecord) do
                     user.save!
                   end

                   lambda { user.sa...
class Stack < Test::Unit::TestCase
                            def set_up()
                              @stack = Stack.n...
describe Stack do
           before(:each) do
             @stack = Stack.new
           end

              it "should be ...
describe Stack do
        context "(empty stack)" do
          it "should be empty"
          it "#pop should raise an exc...
Tuesday, April 27, 2010
1. Unit test-ове
                    2. Test-Driven
                    3. Behavior-Driven
                    4. RSpec
  ...
Feature: Manage posts
                    In order to be able to spread wisdom
                    the blogger
           ...
Given /^I am logged in$/ do
        end

        When /^I go to (.*?)$/ do |page|
        end

        When /^I fill "([^"...
Tuesday, April 27, 2010
Upcoming SlideShare
Loading in …5
×

Behavior-Driven Development с RSpec и Cucumber

1,302 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,302
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
11
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Behavior-Driven Development с RSpec и Cucumber

  1. 1. Behavior-Driven Development с RSpec и Cucumber Стефан Кънев http://skanev.com/ 27.06.2009, TechCamp.eu Tuesday, April 27, 2010
  2. 2. Автоматизирано тестване Tuesday, April 27, 2010
  3. 3. Unit Tests Test-Driven Development Behavior-Driven Development Tuesday, April 27, 2010
  4. 4. 1. Unit test-ове 2. Test-Driven 3. Behavior-Driven 4. RSpec 5. Cucumber Tuesday, April 27, 2010
  5. 5. 1. Unit test-ове 2. Test-Driven 3. Behavior-Driven 4. RSpec 5. Cucumber Tuesday, April 27, 2010
  6. 6. class Programmer def implement_a_change(project, change) files = self.open_related_files(project, change) while true self.attempt_change(change, files) project.run() result = self.click_around_and_test(project) project.stop() break if result.successful? end self.commit_code(files) self.hope_everything_went_ok() end end Tuesday, April 27, 2010
  7. 7. Repetitive... Tuesday, April 27, 2010
  8. 8. ...boring... Tuesday, April 27, 2010
  9. 9. ...error-prone Tuesday, April 27, 2010
  10. 10. Машина, която да го прави вместо нас Tuesday, April 27, 2010
  11. 11. Програма, която да го прави вместо нас Tuesday, April 27, 2010
  12. 12. Unit Test Tuesday, April 27, 2010
  13. 13. Пишем код, който проверява дали друг код работи правилно. Tuesday, April 27, 2010
  14. 14. С един бутон Да Не Tuesday, April 27, 2010
  15. 15. caladan:trane aquarius$ rake spec (in /work/trane) ............................................................... ............................................................... ............................................................... .................. Finished in 8.334414 seconds 207 examples, 0 failures Tuesday, April 27, 2010
  16. 16. caladan:trane aquarius$ rake spec (in /work/trane) ................................................................ ................................................................ ................................................................ .....F......... 1) 'User should know something' FAILED expected: "something", got: "nothing" (using ==) ./spec/models/user_spec.rb:32: Finished in 8.123368 seconds 207 examples, 1 failure Tuesday, April 27, 2010
  17. 17. I can has value? Tuesday, April 27, 2010
  18. 18. Време Tuesday, April 27, 2010
  19. 19. Regression testing Tuesday, April 27, 2010
  20. 20. Tuesday, April 27, 2010
  21. 21. Tuesday, April 27, 2010
  22. 22. Tuesday, April 27, 2010
  23. 23. Tuesday, April 27, 2010
  24. 24. Документация (развален английски + стари версии = fun) Tuesday, April 27, 2010
  25. 25. Спецификация Tuesday, April 27, 2010
  26. 26. Tuesday, April 27, 2010
  27. 27. 1. Unit test-ове 2. Test-Driven 3. Behavior-Driven 4. RSpec 5. Cucumber Tuesday, April 27, 2010
  28. 28. Unit test-овете са просто инструмент Tuesday, April 27, 2010
  29. 29. Test-Driven Development е подход към писането на софтуер Tuesday, April 27, 2010
  30. 30. test-first Tuesday, April 27, 2010
  31. 31. red/green/refactor Tuesday, April 27, 2010
  32. 32. red/green/refactor Възможно най-простия тест, който проверява функцио- налността и не минава. Tuesday, April 27, 2010
  33. 33. red/green/refactor Най-простата имплементация, която кара тестът да минава успешно. Tuesday, April 27, 2010
  34. 34. red/green/refactor Отстраняване на повторения, докато кодът не стане достатъчно качествен. Tuesday, April 27, 2010
  35. 35. Tuesday, April 27, 2010
  36. 36. малки стъпки Tuesday, April 27, 2010
  37. 37. Test-Driven Design Tuesday, April 27, 2010
  38. 38. Преизползваемост Tuesday, April 27, 2010
  39. 39. Low coupling Tuesday, April 27, 2010
  40. 40. Добър интерфейс Tuesday, April 27, 2010
  41. 41. scope creep Tuesday, April 27, 2010
  42. 42. up-front design Tuesday, April 27, 2010
  43. 43. 1. Unit test-ове 2. Test-Driven 3. Behavior-Driven 4. RSpec 5. Cucumber Tuesday, April 27, 2010
  44. 44. Test-Driven Development is not about testing Tuesday, April 27, 2010
  45. 45. TDD ≠ QA Tuesday, April 27, 2010
  46. 46. Опитните TDD-ри използват процеса за да открият поведението, което системата трябва да има. Tuesday, April 27, 2010
  47. 47. Behavior-Driven Development Tuesday, April 27, 2010
  48. 48. BDD ≈ TDD++ Tuesday, April 27, 2010
  49. 49. "Getting the words right" Tuesday, April 27, 2010
  50. 50. Test-Driven Behavior-Driven Tuesday, April 27, 2010
  51. 51. Assertion Expectation Tuesday, April 27, 2010
  52. 52. Test Method Code Example Tuesday, April 27, 2010
  53. 53. Test Case Example Group Tuesday, April 27, 2010
  54. 54. Domain-Driven Design Tuesday, April 27, 2010
  55. 55. Domain Driven Design Eric Evans Tuesday, April 27, 2010
  56. 56. Близки до проблемната област думи Tuesday, April 27, 2010
  57. 57. Разбираем за бизнеса език Tuesday, April 27, 2010
  58. 58. BDD = TDD + DDD (TLA FTW) Tuesday, April 27, 2010
  59. 59. Tuesday, April 27, 2010
  60. 60. BDD = TDD + DDD Tuesday, April 27, 2010
  61. 61. BDD = TDD + DDD BDD = (T + D)DD Tuesday, April 27, 2010
  62. 62. BDD = TDD + DDD BDD = (T + D)DD B=T+D Tuesday, April 27, 2010
  63. 63. BDD = TDD + DDD BDD = (T + D)DD B=T+D интензитет = време + доза Tuesday, April 27, 2010
  64. 64. 1. Unit test-ове 2. Test-Driven 3. Behavior-Driven 4. RSpec 5. Cucumber Tuesday, April 27, 2010
  65. 65. Друг "синтаксис" Tuesday, April 27, 2010
  66. 66. Друг "синтаксис" xUnit assert_equal 42, answer Tuesday, April 27, 2010
  67. 67. Друг "синтаксис" xUnit assert_equal 42, answer RSpec answer.should equal(42) Tuesday, April 27, 2010
  68. 68. Друг "синтаксис" xUnit assert_equal 42, answer RSpec answer.should equal(42) English Answer should equal 42 Tuesday, April 27, 2010
  69. 69. Tuesday, April 27, 2010
  70. 70. assert_equal 42, result Tuesday, April 27, 2010
  71. 71. assert_equal 42, result result.should == 42 Tuesday, April 27, 2010
  72. 72. assert_equal 42, result result.should == 42 assert result.nil? Tuesday, April 27, 2010
  73. 73. assert_equal 42, result result.should == 42 assert result.nil? result.should be_nil Tuesday, April 27, 2010
  74. 74. assert_equal 42, result result.should == 42 assert result.nil? result.should be_nil assert result.kind_of?(User) Tuesday, April 27, 2010
  75. 75. assert_equal 42, result result.should == 42 assert result.nil? result.should be_nil assert result.kind_of?(User) result.should be_kind_of(User) Tuesday, April 27, 2010
  76. 76. assert_equal 42, result result.should == 42 assert result.nil? result.should be_nil assert result.kind_of?(User) result.should be_kind_of(User) assert [], blog.posts Tuesday, April 27, 2010
  77. 77. assert_equal 42, result result.should == 42 assert result.nil? result.should be_nil assert result.kind_of?(User) result.should be_kind_of(User) assert [], blog.posts blog.should have(:no).posts Tuesday, April 27, 2010
  78. 78. assert_equal 42, result result.should == 42 assert result.nil? result.should be_nil assert result.kind_of?(User) result.should be_kind_of(User) assert [], blog.posts blog.should have(:no).posts assert 5, blog.posts.size Tuesday, April 27, 2010
  79. 79. assert_equal 42, result result.should == 42 assert result.nil? result.should be_nil assert result.kind_of?(User) result.should be_kind_of(User) assert [], blog.posts blog.should have(:no).posts assert 5, blog.posts.size blog.should have(5).posts Tuesday, April 27, 2010
  80. 80. Tuesday, April 27, 2010
  81. 81. assert_raise(InvalidRecord) do user.save! end Tuesday, April 27, 2010
  82. 82. assert_raise(InvalidRecord) do user.save! end lambda { user.save! }.should raise_error(InvalidRecord) Tuesday, April 27, 2010
  83. 83. assert_raise(InvalidRecord) do user.save! end lambda { user.save! }.should raise_error(InvalidRecord) assert_difference "User.count", 1 do user.create! end Tuesday, April 27, 2010
  84. 84. assert_raise(InvalidRecord) do user.save! end lambda { user.save! }.should raise_error(InvalidRecord) assert_difference "User.count", 1 do user.create! end lambda { user.save! }.should change { User.count }.by(1) Tuesday, April 27, 2010
  85. 85. class Stack < Test::Unit::TestCase def set_up() @stack = Stack.new end def test_empty assert @stack.empty? @stack.push 42 assert !@stack.empty? end def test_pop @stack.push 42 @stack.push 5 assert_equal 5, @stack.pop assert_equal 42, @stack.pop asser_raise(StackEmptyError) { @stack.pop } end end Tuesday, April 27, 2010
  86. 86. describe Stack do before(:each) do @stack = Stack.new end it "should be able to tell whether it is empty" do @stack.should be_empty @stack.push 42 @stack.should_not be_empty? end it "should pop the items in reverse order" do @stack.push 42 @stack.push 5 @stack.pop.should == 5 @stack.pop.should == 42 lambda { @stack.pop }.should raise_error(StackEmptyError) end end Tuesday, April 27, 2010
  87. 87. describe Stack do context "(empty stack)" do it "should be empty" it "#pop should raise an exception" end context "(non-empty stack)" do it "should not be empty" it "#pop should return the top element" it "#pop should remove the top element" end end Tuesday, April 27, 2010
  88. 88. Tuesday, April 27, 2010
  89. 89. 1. Unit test-ове 2. Test-Driven 3. Behavior-Driven 4. RSpec 5. Cucumber Tuesday, April 27, 2010
  90. 90. Feature: Manage posts In order to be able to spread wisdom the blogger wants to be able to write about anything he wants Scenario: Writing a new post Given that there are no posts And I am logged in When I go to the new post page And I fill "Title" with "F1rst p0st" And I fill "Content" with "This is my first post" And I press "Publish" Then I should be on the front page And I should see "This is my first post" And there should be a new post called "F1rst p0st" Tuesday, April 27, 2010
  91. 91. Given /^I am logged in$/ do end When /^I go to (.*?)$/ do |page| end When /^I fill "([^"]*)" with "([^"]*)"$/ do |field, content| end When /^I press "([^"]*)"$/ do |button_name| end Then /^I should be on (.*?)$/ do |page| end Then /^I should see "([^"]*)"$/ do |text1| end Then /^there should be a new post called "([^"]*)"$/ do |name| end Tuesday, April 27, 2010
  92. 92. Tuesday, April 27, 2010

×