Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Petri Nets Based Testing

Petri Nets Based Testing

A talk about automated exploratory testing based on Petri net models for Saint P RubyConf 2018. Pretty much the English version of https://speakerdeck.com/p0deje/tiestirovaniie-na-osnovie-sietiei-pietri.

Alex Rodionov

June 10, 2018
Tweet

More Decks by Alex Rodionov

Other Decks in Programming

Transcript

  1. 1. Insuf fi ciency of usual tests 2. Model-based testing

    3. Petri nets 4. Petri nets based testing
  2. 1. Insuf fi ciency of usual tests 2. Model-based testing

    3. Petri nets 4. Petri nets based testing
  3. 20 class User < ApplicationRecord end it 'destroys user’ do

    user = User.create! user.destroy expect(User.count).to eq(0) end
  4. 21 arrange act assert class User < ApplicationRecord end it

    'destroys user’ do user = User.create! user.destroy expect(User.count).to eq(0) end
  5. 22 class User < ApplicationRecord end it 'destroys user’ do

    user = User.create! user.destroy expect(User.count).to eq(0) end
  6. 23 class User < ApplicationRecord has_many :notes end class Note

    < ApplicationRecord belongs_to :user end it 'destroys user’ do user = User.create! user.destroy expect(User.count).to eq(0) end
  7. 24 class User < ApplicationRecord has_many :notes end class Note

    < ApplicationRecord belongs_to :user end it 'destroys user with a note’ do user = User.create!(note: Note.create!) user.destroy expect(User.count).to eq(0) end
  8. 25 class User < ApplicationRecord has_many :notes end class Note

    < ApplicationRecord belongs_to :user end it 'destroys user with a note’ do user = User.create!(note: Note.create!) user.destroy expect(User.count).to eq(0) end
  9. 26 class User < ApplicationRecord has_many :notes end class Note

    < ApplicationRecord belongs_to :user end ActiveRecord : : InvalidForeignKey: PG : : ForeignKeyViolation: ERROR : update or delete on table "users" violates foreign key constraint “fk_rails_83d9817008" on table "notes" DETAIL : Key (id)=(1) is still referenced from table “notes".
  10. 27 class User < ApplicationRecord has_many :notes, dependent: :destroy end

    class Note < ApplicationRecord belongs_to :user end it 'destroys user with a note’ do user = User.create!(note: Note.create!) user.destroy expect(User.count).to eq(0) end
  11. 28 class User < ApplicationRecord has_many :notes, dependent: :destroy end

    class Note < ApplicationRecord belongs_to :user end it 'destroys user with a note’ do user = User.create!(note: Note.create!) user.destroy expect(User.count).to eq(0) end
  12. 1. Insuf fi ciency of usual tests 2. Model-based testing

    3. Petri nets 4. Petri nets based testing
  13. 1. Insuf fi ciency of usual tests 2. Model-based testing

    3. Petri nets 4. Petri nets based testing
  14. 61 places store tokens arcs connect places and transitions transitions

    move tokens not created Create User created PETRI NET
  15. 62 places store tokens arcs connect places and transitions transitions

    move tokens not created Create User created PETRI NET
  16. 63 places store tokens arcs connect places and transitions transitions

    move tokens not created Create User created PETRI NET
  17. 65 transition Create User is enabled (can fi re) not

    created Create User created PETRI NET
  18. 66 fi ring removes token from not created and adds

    token to created not created Create User created PETRI NET
  19. 68 1. Places can only be connected to transitions (and

    vice versa). 2. Transitions can fi re if every incoming place has at least one token. PETRI NET
  20. 69 1. Places can only be connected to transitions (and

    vice versa). 2. Transitions can fi re if every incoming place has at least one token. 3. Firing transition consumes (removes) one token from each incoming place and produces (adds) one token to each outgoing place. PETRI NET
  21. 71 not created Create User created ip allowed not created

    Create User created ip allowed PETRI NET
  22. 73 pending review not created Create User created pending review

    not created Create User created PETRI NET
  23. 81 user not created user created note created Create User

    Destroy User Create Note Destroy Note PETRI NET
  24. 82 user not created user created note created Create User

    Destroy User Create Note Destroy Note PETRI NET
  25. 83 user not created user created note created Create User

    Destroy User Create Note Destroy Note PETRI NET
  26. 84 user not created user created note created Create User

    Destroy User Create Note Destroy Note PETRI NET
  27. 86 user not created user created note created Create User

    Destroy User Create Note Destroy Note post created PETRI NET
  28. 87 user not created user created note created Create User

    Destroy User Create Note Destroy Note post created Create Post PETRI NET
  29. 88 user not created user created note created Create User

    Destroy User Create Note Destroy Note post created Create Post Destroy Post PETRI NET
  30. 90 user not created user created note created Create User

    Destroy User Create Note Destroy Note post created Create Post Destroy Post video created PETRI NET
  31. 91 user not created user created note created Create User

    Destroy User Create Note Destroy Note post created Create Post Destroy Post Create Video video created PETRI NET
  32. 92 user not created user created note created Create User

    Destroy User Create Note Destroy Note post created Create Post Destroy Post Create Video Destroy Video video created PETRI NET
  33. 1. Insuf fi ciency of usual tests 2. Model-based testing

    3. Petri nets 4. Petri nets based testing
  34. 1.Create model of the system using Petri net. 2.Generate tests

    based on this model. 96 PETRI NETS BASED TESTING
  35. 98 { "places": [ { "guid": "e57c0830-148f-11e8-bb7f - adf9986e1043", "identifier":

    "user created" } ], "transitions": [ { "guid": “df65a6e0-148f-11e8-bb7f - adf9986e1043" "identifier": "Create User", } ], "arcs": [ { "from_guid": "e57c0830-148f-11e8-bb7f - adf9986e1043", "to_guid": “df65a6e0-148f-11e8-bb7f - adf9986e1043" } ] } CREATE MODEL
  36. 100 class Node def input_arcs # .. . end def

    output_arcs # .. . end end LOAD NET
  37. 101 class Node def input_arcs # .. . end def

    output_arcs # .. . end end class Place < Node end LOAD NET
  38. 102 class Node def input_arcs # .. . end def

    output_arcs # .. . end end class Place < Node end class Transition < Node end LOAD NET
  39. 103 class Node def input_arcs # .. . end def

    output_arcs # .. . end end class Place < Node end class Transition < Node end class Arc def from # .. . end def to # .. . end end LOAD NET
  40. 104 class Runner def enabled?(transition) # .. . end def

    fire(transition) # .. . end def marking # .. . end end LOAD NET
  41. 106 user not created user created note created Create User

    Destroy User Create Note Destroy Note LINK NET TO TEST CODE
  42. 107 user not created user created note created Create User

    Destroy User Create Note Destroy Note class UserNoteNet < Rhizome :: Net end
  43. 108 user not created user created note created Create User

    Destroy User Create Note Destroy Note class UserNoteNet < Rhizome :: Net runner { Runner.new('user - note.bpf', marking: ['user not created']) } end
  44. 109 class UserNoteNet < Rhizome :: Net runner { Runner.new('user

    - note.bpf', marking: ['user not created']) } transition('Create User') { @user = User.create! } transition('Destroy User') { @user.destroy } end user not created user created note created Create User Destroy User Create Note Destroy Note
  45. 110 class UserNoteNet < Rhizome :: Net runner { Runner.new('user

    - note.bpf', marking: ['user not created']) } transition('Create User') { @user = User.create! } transition('Destroy User') { @user.destroy } transition('Create Note') { @note = Note.create!(user: @user) } transition('Destroy Note') { @note.destroy } end user not created user created note created Create User Destroy User Create Note Destroy Note
  46. 111 class UserNoteNet < Rhizome :: Net runner { Runner.new('user

    - note.bpf', marking: ['user not created']) } transition('Create User') { @user = User.create! } transition('Destroy User') { @user.destroy } transition('Create Note') { @note = Note.create!(user: @user) } transition('Destroy Note') { @note.destroy } place('user not created') do empty { expect(User.count).to be > 0 } filled { expect(User.count).to eq(0) } end end user not created user created note created Create User Destroy User Create Note Destroy Note
  47. 112 class UserNoteNet < Rhizome :: Net runner { Runner.new('user

    - note.bpf', marking: ['user not created']) } transition('Create User') { @user = User.create! } transition('Destroy User') { @user.destroy } transition('Create Note') { @note = Note.create!(user: @user) } transition('Destroy Note') { @note.destroy } place('user not created') do empty { expect(User.count).to be > 0 } filled { expect(User.count).to eq(0) } end place('user created') do empty { expect(User.count).to eq(0) } filled { expect(User.count).to be > 0 } end end user not created user created note created Create User Destroy User Create Note Destroy Note
  48. class UserNoteNet < Rhizome :: Net runner { Runner.new('user -

    note.bpf', marking: ['user not created']) } transition('Create User') { @user = User.create! } transition('Destroy User') { @user.destroy } transition('Create Note') { @note = Note.create!(user: @user) } transition('Destroy Note') { @note.destroy } place('user not created') do empty { expect(User.count).to be > 0 } filled { expect(User.count).to eq(0) } end place('user created') do empty { expect(User.count).to eq(0) } filled { expect(User.count).to be > 0 } end place('note created') do empty { expect(Note.count).to eq(0) } filled { expect(Note.count).to be > 0 } end end user not created user created note created Create User Destroy User Create Note Destroy Note 113
  49. TRAVERSE NET 1.Give every transition same probability (4 transitions =

    0.25 to each). 2.Generate random number - a threshold of transition selection. 115
  50. TRAVERSE NET 1.Give every transition same probability (4 transitions =

    0.25 to each). 2.Generate random number - a threshold of transition selection. 3.Select the fi rst transition which is enabled and has probability suitable for the threshold. 116
  51. TRAVERSE NET 1.Give every transition same probability (4 transitions =

    0.25 to each). 2.Generate random number - a threshold of transition selection. 3.Select the fi rst transition which is enabled and has probability suitable for the threshold. 4.Fire transition. 117
  52. TRAVERSE NET 1.Give every transition same probability (4 transitions =

    0.25 to each). 2.Generate random number - a threshold of transition selection. 3.Select the fi rst transition which is enabled and has probability suitable for the threshold. 4.Fire transition. 5.Verify empty and fi lled places. 118
  53. TRAVERSE NET 1.Give every transition same probability (4 transitions =

    0.25 to each). 2.Generate random number - a threshold of transition selection. 3.Select the fi rst transition which is enabled and has probability suitable for the threshold. 4.Fire transition. 5.Verify empty and fi lled places. 6.Halve selected transition probability. 119
  54. TRAVERSE NET 1.Give every transition same probability (4 transitions =

    0.25 to each). 2.Generate random number - a threshold of transition selection. 3.Select the fi rst transition which is enabled and has probability suitable for the threshold. 4.Fire transition. 5.Verify empty and fi lled places. 6.Halve selected transition probability. 7.If all transitions fi red at least once - stop. Otherwise, repeat from point 2. 120
  55. 122 $ bin/rhizome Traversing user_note net: Initial marking: user not

    created Executing Create User New marking: user created Transitions left: Destroy User, Create Note, Destroy Note
  56. 123 $ bin/rhizome Traversing user_note net: Initial marking: user not

    created Executing Create User New marking: user created Transitions left: Destroy User, Create Note, Destroy Note Executing Create Note New marking: user created, note created Transitions left: Destroy User, Destroy Note
  57. 124 $ bin/rhizome Traversing user_note net: Initial marking: user not

    created Executing Create User New marking: user created Transitions left: Destroy User, Create Note, Destroy Note Executing Create Note New marking: user created, note created Transitions left: Destroy User, Destroy Note Executing Destroy Note New marking: user created Transitions left: Destroy User
  58. 125 $ bin/rhizome Traversing user_note net: Initial marking: user not

    created Executing Create User New marking: user created Transitions left: Destroy User, Create Note, Destroy Note Executing Create Note New marking: user created, note created Transitions left: Destroy User, Destroy Note Executing Destroy Note New marking: user created Transitions left: Destroy User Executing Destroy User New marking: user not created Finished traversing user_note net.
  59. 127 $ bin/rhizome Traversing user_note net: Initial marking: user not

    created Executing Create User New marking: user created Transitions left: Destroy User, Create Note, Destroy Note
  60. 128 $ bin/rhizome Traversing user_note net: Initial marking: user not

    created Executing Create User New marking: user created Transitions left: Destroy User, Create Note, Destroy Note Executing Destroy User New marking: user not created Transitions left: Create Note, Destroy Note
  61. 129 $ bin/rhizome Traversing user_note net: Initial marking: user not

    created Executing Create User New marking: user created Transitions left: Destroy User, Create Note, Destroy Note Executing Destroy User New marking: user not created Transitions left: Create Note, Destroy Note Executing Create User New marking: user created Transitions left: Create Note, Destroy Note
  62. 130 $ bin/rhizome Traversing user_note net: Initial marking: user not

    created Executing Create User New marking: user created Transitions left: Destroy User, Create Note, Destroy Note Executing Destroy User New marking: user not created Transitions left: Create Note, Destroy Note Executing Create User New marking: user created Transitions left: Create Note, Destroy Note Executing Create Note New marking: user created, note created Transitions left: Destroy Note
  63. 131 $ bin/rhizome Traversing user_note net: Initial marking: user not

    created Executing Create User New marking: user created Transitions left: Destroy User, Create Note, Destroy Note Executing Destroy User New marking: user not created Transitions left: Create Note, Destroy Note Executing Create User New marking: user created Transitions left: Create Note, Destroy Note Executing Create Note New marking: user created, note created Transitions left: Destroy Note Executing Destroy Note New marking: user created Finished traversing user_note net.
  64. 133 $ bin/rhizome Traversing user_note net: Initial marking: user not

    created Executing Create User New marking: user created Transitions left: Destroy User, Create Note, Destroy Note
  65. 134 $ bin/rhizome Traversing user_note net: Initial marking: user not

    created Executing Create User New marking: user created Transitions left: Destroy User, Create Note, Destroy Note Executing Create Note New marking: user created, note created Transitions left: Destroy User, Destroy Note
  66. 135 $ bin/rhizome Traversing user_note net: Initial marking: user not

    created Executing Create User New marking: user created Transitions left: Destroy User, Create Note, Destroy Note Executing Create Note New marking: user created, note created Transitions left: Destroy User, Destroy Note Executing Destroy User 
 ActiveRecord :: InvalidForeignKey: PG : : ForeignKeyViolation: ERROR : update or delete on table "users" violates foreign key constraint "fk_rails_83d9817008" on table "notes" DETAIL : Key (id)=(1) is still referenced from table “notes".
  67. RHIZOME 1. Reproducible tests using seed. 2. Parallelize like any

    other test framework (parallel_tests). 137
  68. RHIZOME 1. Reproducible tests using seed. 2. Parallelize like any

    other test framework (parallel_tests). 3. General purpose framework (API, UI). 138
  69. 1. Test coverage increased. 2. Time to run tests is

    the same. 143 PETRI NETS BASED TESTING
  70. 1. Test coverage increased. 2. Time to run tests is

    the same. 3. Time to write and support tests decreased. 144 PETRI NETS BASED TESTING
  71. 146 user not created user created note created Create User

    Destroy User Create Note Destroy Note
  72. 147 user not created user created note created Create User

    Destroy User Create Note Destroy Note
  73. 148 user not created user created note created Create User

    Destroy User Create Note Destroy Note
  74. 149 user not created user created note created Create User

    Destroy User Create Note Destroy Note
  75. 150 user not created user created note created Create User

    Destroy User Create Note Destroy Note
  76. RESET ARC 151 user not created user created note created

    Create User Destroy User Create Note Destroy Note clears place after fi ring
  77. INHIBITOR ARC 152 user created note created Create User Destroy

    User Create Note Destroy Note enables transition only if connected place has no tokens
  78. user not created user created Create User Destroy User HIERARCHICAL

    PETRI NET 153 Note note created Create Note Destroy Note
  79. PETRI NET 154 user not created user created note created

    Create User Destroy User Create Note Destroy Note
  80. COLORED PETRI NET 155 user not created user created note

    created Create User Destroy User Create Note Destroy Note token stores data