Rspec, Pry, Automation Testing
Total Page:16
File Type:pdf, Size:1020Kb
GKTCS Innovations Pvt. Ltd. Ruby, Appium, Android, Rspec, Pry, Automation Testing Surendra Panpaliya Director, GKTCS Innovations Pvt. Ltd, Pune. 16 + Years of Experience ( MCA, PGDCS, BSc. [Electronics] , CCNA) Director , GKTCS Innovations Pvt. Ltd. Pune [ Nov 2009 – Till date ] IT Manager and Consultant at Rolta India Ltd, Mumbai [ April 2007 – Oct 2009 ] Associate Professor at Sinhgad Institute , Pune [Jan 2002 – April 2007] Instructor , ITM Bangalore [ May 2000 – Jan 2002 ] Project Trainee, DSYS Software Group, Bangalore [ Jan 1999 to April 2000] Skills ❑ Ruby, Rail,Appium, Python, Jython, Django, Android , PHP, LAMP ❑ Data Communication & Networking, CCNA ❑ UNIX /Linux Shell Scripting, System Programming ❑ CA Siteminder, Autosys, SSO, Service Desk, Service Delivery Author of 4 Books National Paper Presentation Awards at BARC Mumbai 2 Agenda ( Day 1) • Introduction • Ruby installation • What’s New in Ruby • Ruby Data Types/ Data Structure • Ruby Comparison with Other Language ( Java/C+ + ) • Ruby Demo Script • Appium Introduction • Appium Installation Setup • Discussion 3 Agenda ( Day 2) • Android adb commands • About Android AVD • About Appium Architecture • Appium Features • Using Appium to test Android Emulator/ Physical Device • Writing Ruby Scripts to Automate app through Appium • Discussion 4 Agenda ( Day 3) • Manual Testing to Automation using Ruby • Execute and Automate Test • Rspec Introduction • Rspec Installation • Rspec for Automation • Rspec Demo • Rspec Best Practices • Pry Introduction • Debugging using Pry • Pry Demo • Discussion 5 Rspec What is Rspec? • RSpec is a behavior-driven development (BDD) framework for the Ruby programming language, inspired by JBehave. • It contains its own mocking framework that is fully integrated into the framework based upon JMock. • The framework can be considered a domain-specific language (DSL) and resembles a natural language specification. 6 Rspec RSpec is a Behaviour-Driven Development tool for Ruby programmers. BDD is an approach to software development that combines Test- Driven Development, Domain Driven Design,and Acceptance Test-Driven Planning. RSpec helps you do the TDD part of that equation,focusing on the documentation and design aspects of TDD. 7 Rspec #Testing for our User class describe User do context 'with admin privileges' do before :each do @admin = Admin.get(1) end it 'should exist' do expect(@admin).not_to be_nil end it 'should have a name' do expect(@admin.name).not_to be_false end end #... end 8 Rspec Prerequisites Ruby 1.8.7 or higher (2.0+ recommended) Install $ gem install rspec This installs five gems: rspec rspec-core rspec-expectations rspec-mocks rspec-support The rspec-core gem installs an rspec executable. Run the rspec command with the --help flag to see the available options: 9 rspec —help Surendras-MacBook-Pro:~ SurendraMac$ rspec --help Usage: rspec [options] [files or directories] -I PATH Specify PATH to add to $LOAD_PATH (may be used more than once). -r, --require PATH Require a file. -O, --options PATH Specify the path to a custom options file. --order TYPE[:SEED] Run examples by the specified order type. [defined] examples and groups are run in the order they are defined [rand] randomize the order of groups and examples [random] alias for rand [random:SEED] e.g. --order random:123 --seed SEED Equivalent of --order rand:SEED. --bisect[=verbose] Repeatedly runs the suite in order to isolate the failures to the smallest reproducible case. --[no-]fail-fast Abort the run on first failure. --failure-exit-code CODE Override the exit code used when there are failing specs. --dry-run Print the formatter output of your suite without running any examples or hooks 10 rspec —help Surendras-MacBook-Pro:~ SurendraMac$ rspec --help **** Output **** -f, --format FORMATTER Choose a formatter. [p]rogress (default - dots) [d]ocumentation (group and example names) [h]tml [j]son custom formatter class name -o, --out FILE Write output to a file instead of $stdout. This option applies to the previously specified --format, or the default format if no format is specified. 11 rspec —help --deprecation-out FILE Write deprecation warnings to a file instead of $stderr. -b, --backtrace Enable full backtrace. -c, --[no-]color, --[no-]colour Enable color in the output. -p, --[no-]profile [COUNT] Enable profiling of examples and list the slowest examples (default: 10). -w, --warnings Enable ruby warnings 12 rspec —help **** Filtering/tags **** In addition to the following options for selecting specific files, groups, or examples, you can select individual examples by appending the line number(s) to the filename: rspec path/to/a_spec.rb:37:87 You can also pass example ids enclosed in square brackets: rspec path/to/a_spec.rb[1:5,1:6] # run the 5th and 6th examples/groups defined in the 1st group --only-failures Filter to just the examples that failed the last time they ran. --next-failure Apply `--only-failures` and abort after one failure. (Equivalent to `--only-failures --fail-fast --order defined`) 13 rspec —help -P, --pattern PATTERN Load files matching pattern (default: "spec/**/*_spec.rb"). --exclude-pattern PATTERN Load files except those matching pattern. Opposite effect of --pattern. -e, --example STRING Run examples whose full nested names include STRING (may be used more than once) -t, --tag TAG[:VALUE] Run examples with the specified tag, or exclude examples by adding ~ before the tag. - e.g. ~slow - TAG is always converted to a symbol --default-path PATH Set the default path where RSpec looks for examples (can be a path to a file or a directory). **** Utility **** -v, --version Display the version. -h, --help You're looking at it. 14 rspec - Getting Ready 2. Next, prepare the directory structure: $ mkdir lib 3. Now run RSpec: $ rspec --init 4 To show what we're testing, we'll change the .rspec file generated for us, replacing progress with doc: - -color --format doc 5. We can now run RSpec on our empty specs directory and verify we have the gem installed: $ rspec spec No examples found. Finished in 0.00004 seconds 0 examples, 0 failures 15 rspec - Getting Ready RSpec creates a spec directory, a spec_helper.rb file within that directory, and a .rspec file in the current directory with sensible defaults. If you have a problem locating the .rspec file, it's because the file is hidden. A command-line editor such as Vim has no problem opening a hidden file. for example vim .rspec, but using a common dialog box to select a hidden file can be difficult. In OS X, while the Open dialog box is shown, you can press command + shift + . (period) to temporarily show these hidden files. 16 rspec - Getting Ready RSpec creates a spec directory, a spec_helper.rb file within that directory, and a .rspec file in the current directory with sensible defaults. If you have a problem locating the .rspec file, it's because the file is hidden. A command-line editor such as Vim has no problem opening a hidden file. for example vim .rspec, but using a common dialog box to select a hidden file can be difficult. In OS X, while the Open dialog box is shown, you can press command + shift + . (period) to temporarily show these hidden files. 17 Getting started Begin with a very simple example that expresses some basic desired behaviour. # game_spec.rb RSpec.describe Game do describe "#score" do it "returns 0 for an all gutter game" do game = Game.new 20.times { game.roll(0) } expect(game.score).to eq(0) end end end 18 Getting started Run the example and watch it fail. Surendras-MacBook-Pro:rspect_test SurendraMac$ rspec spec/game_spec.rb /Users/SurendraMac/rspect_test/spec/ game_spec.rb:3:in `<top (required)>': uninitialized constant Game (NameError) from /usr/local/lib/ruby/gems/2.2.0/gems/rspec- core-3.3.2/lib/rspec/core/configuration.rb: 1327:in `load' from /usr/local/lib/ruby/gems/2.2.0/gems/rspec- core-3.3.2/lib/rspec/core/configuration.rb: 1327:in `block in load_spec_files' 19 Getting started Now write just enough code to make it pass. require ‘game' RSpec.describe Game do describe "#score" do it "returns 0 for an all gutter game" do game = Game.new 20.times { game.roll(0) } expect(game.score).to eq(0) end end end 20 Getting started # game.rb class Game def roll(pins) end def score 0 end end Surendras-MacBook-Pro:rspect_test SurendraMac$ rspec spec/game_spec.rb Finished in 0.00108 seconds (files took 0.09666 seconds to load) 1 example, 0 failures 21 Getting started Run the example and bask in the joy that is green. $rspec spec/game_spec.rb --color --format doc Game #score returns 0 for an all gutter game Finished in 0.0008 seconds (files took 0.08722 seconds to load) 1 example, 0 failures Surendras-MacBook-Pro:rspect_test SurendraMac$ 22 rspec-core rspec-core provides the structure for writing executable examples of how your code should behave, and an rspec command with tools to constrain which examples get run and tailor the output. Install gem install rspec # for rspec-core, rspec-expectations, rspec-mocks gem install rspec-core # for rspec-core only rspec --help 23 rspec-core Want to run against the master branch? You'll need to include the dependent RSpec repos as well. Add the following to your Gemfile: %w[rspec rspec-core rspec-expectations rspec-mocks rspec-support].each do |lib| gem lib, :git => "git://github.com/rspec/#{lib}.git", :branch => 'master' end 24 rspec-core :Basic Structure RSpec uses the words "describe" and "it" so we can express concepts like a conversation: "Describe an order." "It sums the prices of its line items." RSpec.describe Order do it "sums the prices of its line items" do order = Order.new order.add_entry(LineItem.new(:item => Item.new( :price => Money.new(1.11, :USD) ))) ……. 25 rspec-core :Basic Structure .. order.add_entry(LineItem.new(:item => Item.new( :price => Money.new(2.22, :USD), :quantity => 2 ))) expect(order.total).to eq(Money.new(5.55, :USD)) end end 26 rspec-core :Basic Structure .