CSC/ECE 517 Fall 2012/ch2a 2w12 sv: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
 
(24 intermediate revisions by the same user not shown)
Line 3: Line 3:
=='''Introduction'''==
=='''Introduction'''==
'''Build Management ''' is the process of delivering the software in an executable format .The final deliverables are known as '''software builds '''. For simple programs one single file is used for compilation but for complex software there will be a main file which in turn will invoke several other files for code compilation.  
'''Build Management ''' is the process of delivering the software in an executable format .The final deliverables are known as '''software builds '''. For simple programs one single file is used for compilation but for complex software there will be a main file which in turn will invoke several other files for code compilation.  
Considering the following example
  A software build may consist of several ‘.c’ and ‘.h’ files. There will be number of make files which will compile the code files. For building the entire software one main Makefile is written which will call other make files for generating the executable.
  The files are compiled and linked in order by the Build Utility. If there are no code changes in a file then recompilation is not required. Moreover complicated build utilities and linkers do not recompile the code to save time required for the entire build generation.
  Build Automation is a component of Build Management and involves automating the activities that developers perform regularly such as compiling source code into binary code and packaging them, running tests and deploying code at the production site and create documentation related to the same.


Consider the following example


A software build may consist of several ‘.c’ and ‘.h’ files. There will be number of make files which will compile the code files. For building the entire software one main Makefile is written which will call other make files for generating the executable.
The files are compiled and linked in order by the Build Utility. If there are no code changes in a file then recompilation is not required. Moreover complicated build utilities and linkers do not recompile the code to save time required for the entire build generation.


Though Cucumber is a testing tool, the main intent of its development is to support [http://en.wikipedia.org/wiki/Behavior-driven_development Behavior Driven Development(BDD)] which is derived from [http://en.wikipedia.org/wiki/Test-driven_development Test Driven Development]. This implies that the tests written in DSL are typically written before any other part of the code is written. It is analysed by the non technical stakeholders and then the production code is written. The production code is thus written outside-in, in such a way that the tests pass.
'''Build Automation''' is a component of Build Management and involves automating the activities that developers perform regularly such as compiling source code into binary code and packaging them, running tests and deploying code at the production site and create documentation related to the same.
 
Cucumber provides support for a wide range of Ruby virtual machines such as [http://en.wikipedia.org/wiki/JRuby JRuby], alternative application frameworks such as [http://www.sinatrarb.com/ Sinatra], other programming languages such as [http://www.python.org Python], test environments such as Capybara and provides [http://www.debian.org/doc/manuals/intro-i18n/ i18n] language support for feature and step files.
 
This article primarily focuses on running Cucumber tests on the Capybara testing environment. It carries all the information about the driver running the test and comes with a built in support for Rack::Test and [http://seleniumhq.org/projects/ide/ Selenium].
 
'''Capybara''' is a testing environment that simulates the manner in which a real world user would interact with the application. It requires no explicit setup for [http://en.wikipedia.org/wiki/Ruby_on_Rails Rails] applications. The most powerful feature of Capybara is that it has a very intuitive application program interface that understands any language an actual user would use. With the libraries and drivers provided by Capybara for deploying web applications and for interacting with service applications, the testing requires minimum amount of coding.


=='''History and the need for development'''==
=='''History and the need for development'''==


Cucumber is actually Aslak Hellesoy's revamp of [http://rspec.info/ RSpec]'s "Story Runner"(by Dan North). The earlier versions of "Story Runner" and its predecessor [http://dannorth.net/2007/06/17/introducing-rbehave/ RBehave] required that the stories be written in Ruby. But seeing the associated inconvenience, David Chelimsky added the plain text support with contributions done from an eclectic support team.  
Build automation tools are used to call compilers and linkers from inside the build script. Prior to build script, there was a need to specify the order of the files which the compiler needs to call from the command line. However , it is easier to pass a single file to a compiler which then reads from it and then linker reads from the single build file to create the deployable object. Using build automation, we write build scripts that are called in series which the compilers and linkers use to build software. Additional features were added which allows source code dependency management as well as incremental build management. With the advancement of build automation, different ways were provided to specify pre-jobs and post jobs. Pre-jobs are the jobs which should be executed before the execution of the actual program. Post-jobs are the jobs, which should be executed after the execution of the jobs given in the build file.
 
In April 2008, Aslak Hellesoy started the Cucumber project to iron out the inherent flaws and glitches of the RSpec Story Runner. Joseph Wilk and Ben Mabey were the regular contributors to the project. Matt Wynne joined in September 2009. In October 2009, Mike Sassak and Gregory Hnatiuk did some great work on a faster parser for Cucumber. In addition to the core team, there were over 250 developers contributing to the overall development of the project.


Though Cucumber is written in Ruby, it can be used to test code written in Ruby and other languages too. Not limited to Java, C# and Python. It required minimal use of Ruby and is hence believed to be extremely easy to code. Moreover, the user stories for the features can be written in any human language.
=='''Build Management Tools'''==
==='''Make'''===


Ever since, Cucumber has seen a lot of revision and refactoring. One of the major refactoring include transferring certain implementation portions to make it customized for particular frameworks
The make command makes use of the Makefile for compiling the code. The Makefile is generally written in [http://www.perl.org/ Perl] or some other scripting language. The wrapper script takes into account the tasks needed for build automation i.e. source code check out, unit tests execution and deployment of the code by placing it in the suitable directory.


=='''Setting up Cucumber'''==
A rule in make file specifies how set of instructions should be executed to generate target files from source files.
==='''Generic installation'''===
A simple rule is of the form
  target: dependencies…
          commands…


Install Ruby on the system
Code can be compiled by typing '''make''' on the command prompt which looks for a Makefile in the present directory and executes it.The build process has two steps in which the compiler first takes the source code and creates object files. The linker then takes the object files and generates an executable.
When executing make the target needs to be specified else it updates the first target mentioned in the Makefile. The intermediate targets are updated before the updation of the final target.The target files should always be up to date. If dependencies are older than the target file, then the target file is already up to date, and it needn’t be generated again.


  gem install cucumber
Here is how a Makefile for compiling kernel modules is
  obj-m += helloWorld.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean


To read up on more information about this gem
The above Makefile compiles a kernel module helloWorld.c .'''all:''' is the default target for all files and make utility executes this if no other target is specified. The '''clean:''' target clears all the compiled code.


cucumber --help
==='''Ant'''===


==='''On Ruby on Rails 3.x'''===
Acronym for '''Another Neat Tool''', ant was developed on a Windows Laptop and now owned by [http://www.apache.org/ Apache]. Ant makes use of XML to describe build processes and corresponding dependencies. By default the build file is given the name build.xml. Ant is useful in building Java applications. Ant can also be used for building non-Java application like C and C++. Ant uses Java to specify the types and tasks to be performed which are stored in “antlibs” . Ant is quite flexible and does not enforce the usage of coding convention and directory layouts to the Java projects which use it as a build tool.
An Ant file for compiling a helloWorld.java program
<?xml version="1.0" encoding="UTF-8"?>
<project name="HW.makejar" default="makejar" basedir=".">
<target name ="makejar" description="Create a jar for the HW project">
  <jar jarfile="HelloWorld.jar" includes="*.class" basedir="bin"/>
</target>
</project>


Cucumber-Rails is used for performing Cucumber tests on Rails.
==='''Maven'''===


Step 1 : Add the following to the Gem File
Apache Maven is a software project management and comprehension tool. Derived from the [http://maven.apache.org/guides/introduction/introduction-to-the-pom.html project object model] (POM), Maven manages project's build, reports and documentation from a central piece of information.
group :test do
  gem 'cucumber-rails', :require => false
  # database_cleaner is not required, but highly recommended
  gem 'database_cleaner'
end


Step 2 : Then install the gem
Maven configuration is effusive, difficult to read and write. Tasks which can be done in one or two lines of Ruby or XML with Rake or Ant require six or seven lines of pom.xml configuration (assuming it’s possible to write it in Maven).
bundle install


Step 3 : Bootstrap your Rails app by
In Maven certain things are configured as classpath references to .properties files bundled in .jar files configured as dependencies, some things are configured as absolute or relative paths to files on disk, and some things are configured as system properties in the JVM running Maven. Sometimes Maven can recursively build projects in the correct order, but sometimes it’s not.


rails generate cucumber:install
==='''Rake'''===


=='''How Cucumber works''' ==
Rake is the best build tool for Java Projects. The common build tasks in Rake are performed using certain standard methods and uses Ruby programming language for specifying the commands in Rakefiles which are equivalent of Makefiles when using make.
Cucumber testing comprises of two components namely
  file 'helloWorld.o' => ['helloWorld.c'] do
*Feature
    sh 'cc -c -o helloWorld.o helloWorld.c'
*Step Definitions
  end
  file 'helloWorld' => ['helloWorld.o'] do
    sh 'cc -o helloWorld helloWorld.o'
  end


==='''Features'''===
Follwing the steps for build process the first sh command creates an object file and then the next sh command creates an executable from object file i.e. helloWorld from helloWorld.o
Features are essentially outward facing in the sense that they describe the ideal outcome given a state of the system. A given state of the system is the cumulative input to the system comprising of the events taking place in the given context and the predefined variables. Each feature is a collection of scenarios. The scenarios each are described by the given/when/then + and/but construct. Features are also known as '''matcher''' methods and are integral link to the step definitions. Now when Cucumber is executed the corresponding block of Ruby code is invoked. Scenarios are independent of each other and once a scenario execution is over there is nothing which is carried over to the next scenario.  


===='''Given/When/Then + And/But construct'''====
==='''Scons'''===


'''Given''' : 
Scons is a Python based language and is acronym for '''Software Construction'''.SConstruct files which are similar to Makefiles are easier to maintain.SCons also supports C/C++ and Java™ files, and as compared to make Scons needs a lot lesser code.
Purpose of given is to create a setting for user interaction with the system. These are analogous to the pre-conditions of [http://en.wikipedia.org/wiki/Use_case Usecase].
 
'''When''':
This for the main actions the user performs ‘given’ a scenario i.e. transition of state which has an observable effect.
 
'''Then''':
This is majorly to check the consequences of the action performed by the user i.e. what is the output of the system when a particular input is given.
 
'''And/But''': 
These are used when there are multiple given conditions for which some actions need to be performed.
 
An Example:
 
  Scenario: Manifold Givens
  Given the username
  And Given the password
  When I press enter
  Then I see the username on the screen
  But I do not see the password on the screen
 
==='''Step Definitions'''===
Step definitions are primarily inward facing as they map the outward facing scenario statements with the inner Ruby code snippets. Step definitions either consists of keyword, string or regular expression. The mapping is mostly done by expression matching and calling the appropriate code snippets in the Ruby code.
 
=='''Setting up Capybara''' ==
As explained before, Capybara doesn't require explict set up for Rails applications.
For Rails, we add the following line to the test helper file:
require 'capybara/rails'
 
=='''Example of running Cucumber tests on Capybara'''==
After setting up cucumber-rails and Capybara as described above, the following example describes how an actual cucumber test is written and run in the Capybara environment. It is also possible to use the Capybara DSL for writing the tests but the most widely used DSL is Gherkin. The example below uses Gherkin but it can be easily transformed using the DSL provided by Capybara.
 
'''Features and Scenarios''': As discussed above, the features ... In the example below, the feature describes a scenario within a banking system where a student needs to test the system's ability to find the simple interest on a given principal at a given rate per year. 
 
# Language: English
Feature: InterestCalc
In order to avoid an error in interest calculation
As a new student
I want to be told the interest for given principal per year
  Scenario: Interest on Principal
  * I have entered 1000 into the interest_calc
  * I have entered  3.5 into the interest_calc
  * I press calc
  * The result should 35 on the screen
 
'''Step Definitions''': As discussed before, the step definitions provided the mapping for the corresponding code snippets that each scenario within the feature interacts with. In this example, for the givens, we first create an object of the class Interest_calc and push the arguments into the instance. Then for when, we call the corresponding method that calculates the interest. For the then, we display the result on the screen.
 
    require 'capybara/cucumber'
    require 'Interest_calc'
    Given /I have entered (.*) into the interest_calc/ do |n|
      @calci=Interest_calc.new
      @calci.setdata(n)
    end
    When /I press (\w+)/ do |op|
      @result=@calci.calc
    end
    Then /the result should be (.*) on the screen/ do |result|
      @result.should == result.to_f
    end
 
'''Class definition''': This is the Interest_calc class that is accessed in the step definition.
 
  class Interest_calc
    def push(n)
      @args ||= []
      @args<<n
    end
    def calc
      @args.inject(0){|p,r| p=p*r/100}
    end
  end
 
=='''Advantages'''==
The advantages of Cucumber are that it mandates a person to spend more time in the planning phase before directly getting into the complexities of models, views and controllers. It lets one have a broader perspective of the application one intends to develop.
The syntax which is in simple English is what makes it unique letting clients who are not aware of the technicalities of a system, voice their opinion in the development process and hence the development is always in sync with the requirement.
 
=='''Ongoing developments'''==
Cucumber has rapidly become one of the most preferred tools for automated testing. Aslak Hellesoy and his team are working on further advancements and improvements to the tool. Improving the documentation via the [http://pragprog.com/book/hwcuc/the-cucumber-book "The Cucumber Book"] being one of them. The book continues to be revised to keep up with its rapid evolution. Currently, Cucumber tests run on JVMs through JRuby. But to overcome the inherent latency, the team is working on a native Java implementation of the tool which can be built in to the Java development environment. During this adaptation to Java, the team also found aspects of the Ruby implementation that could be improved to make it much more flexible.


=='''References'''==
=='''References'''==
*http://cukes.info/
*http://en.wikipedia.org/wiki/Software_build
*http://www.rubyinside.com/cucumber-the-latest-in-ruby-testing-1342.html
*http://www.luxoft.com/downloads/white_papers/Build%20Management%20Software.pdf
*http://www.allenwei.cn/cucumber-capybara-what-we-need-for-rails-integration-test/
*http://www.meritalk.com/uploads_legacy/whitepapers/Build%20management.pdf (we can check on electric cloud)
*http://railscasts.com/episodes/155-beginning-with-cucumber
*http://www.ibm.com/developerworks/aix/library/au-nextgeneration_build/index.html
*http://rubysource.com/smelly-cucumbers/
*http://www.gnu.org/software/make/
 
*http://www2.cs.siu.edu/~rahimi/cs412/slides/What%20is%20Ant%20.htm
=='''Further reading'''==
*http://kent.spillner.org/blog/work/2009/11/14/java-build-tools.html
*http://blog.objectmentor.com/articles/2008/11/27/the-truth-about-bdd
*http://en.wikipedia.org/wiki/Apache_Ant
*http://jimmyzimmerman.com/blog/2008/05/intro-to-story-runner-and-bdd.html
*http://en.wikipedia.org/wiki/Build_automation

Latest revision as of 22:05, 27 October 2012

Build Management

Introduction

Build Management is the process of delivering the software in an executable format .The final deliverables are known as software builds . For simple programs one single file is used for compilation but for complex software there will be a main file which in turn will invoke several other files for code compilation.

Consider the following example

A software build may consist of several ‘.c’ and ‘.h’ files. There will be number of make files which will compile the code files. For building the entire software one main Makefile is written which will call other make files for generating the executable. The files are compiled and linked in order by the Build Utility. If there are no code changes in a file then recompilation is not required. Moreover complicated build utilities and linkers do not recompile the code to save time required for the entire build generation.

Build Automation is a component of Build Management and involves automating the activities that developers perform regularly such as compiling source code into binary code and packaging them, running tests and deploying code at the production site and create documentation related to the same.

History and the need for development

Build automation tools are used to call compilers and linkers from inside the build script. Prior to build script, there was a need to specify the order of the files which the compiler needs to call from the command line. However , it is easier to pass a single file to a compiler which then reads from it and then linker reads from the single build file to create the deployable object. Using build automation, we write build scripts that are called in series which the compilers and linkers use to build software. Additional features were added which allows source code dependency management as well as incremental build management. With the advancement of build automation, different ways were provided to specify pre-jobs and post jobs. Pre-jobs are the jobs which should be executed before the execution of the actual program. Post-jobs are the jobs, which should be executed after the execution of the jobs given in the build file.

Build Management Tools

Make

The make command makes use of the Makefile for compiling the code. The Makefile is generally written in Perl or some other scripting language. The wrapper script takes into account the tasks needed for build automation i.e. source code check out, unit tests execution and deployment of the code by placing it in the suitable directory.

A rule in make file specifies how set of instructions should be executed to generate target files from source files.

A simple rule is of the form
 target: dependencies…
         commands…

Code can be compiled by typing make on the command prompt which looks for a Makefile in the present directory and executes it.The build process has two steps in which the compiler first takes the source code and creates object files. The linker then takes the object files and generates an executable. When executing make the target needs to be specified else it updates the first target mentioned in the Makefile. The intermediate targets are updated before the updation of the final target.The target files should always be up to date. If dependencies are older than the target file, then the target file is already up to date, and it needn’t be generated again.

Here is how a Makefile for compiling kernel modules is

obj-m += helloWorld.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean

The above Makefile compiles a kernel module helloWorld.c .all: is the default target for all files and make utility executes this if no other target is specified. The clean: target clears all the compiled code.

Ant

Acronym for Another Neat Tool, ant was developed on a Windows Laptop and now owned by Apache. Ant makes use of XML to describe build processes and corresponding dependencies. By default the build file is given the name build.xml. Ant is useful in building Java applications. Ant can also be used for building non-Java application like C and C++. Ant uses Java to specify the types and tasks to be performed which are stored in “antlibs” . Ant is quite flexible and does not enforce the usage of coding convention and directory layouts to the Java projects which use it as a build tool. An Ant file for compiling a helloWorld.java program

<?xml version="1.0" encoding="UTF-8"?>
<project name="HW.makejar" default="makejar" basedir=".">
<target name ="makejar" description="Create a jar for the HW project">
  <jar jarfile="HelloWorld.jar" includes="*.class" basedir="bin"/>
</target>
</project>

Maven

Apache Maven is a software project management and comprehension tool. Derived from the project object model (POM), Maven manages project's build, reports and documentation from a central piece of information.

Maven configuration is effusive, difficult to read and write. Tasks which can be done in one or two lines of Ruby or XML with Rake or Ant require six or seven lines of pom.xml configuration (assuming it’s possible to write it in Maven).

In Maven certain things are configured as classpath references to .properties files bundled in .jar files configured as dependencies, some things are configured as absolute or relative paths to files on disk, and some things are configured as system properties in the JVM running Maven. Sometimes Maven can recursively build projects in the correct order, but sometimes it’s not.

Rake

Rake is the best build tool for Java Projects. The common build tasks in Rake are performed using certain standard methods and uses Ruby programming language for specifying the commands in Rakefiles which are equivalent of Makefiles when using make.

 file 'helloWorld.o' => ['helloWorld.c'] do
   sh 'cc -c -o helloWorld.o helloWorld.c'
 end
 file 'helloWorld' => ['helloWorld.o'] do
   sh 'cc -o helloWorld helloWorld.o'
 end

Follwing the steps for build process the first sh command creates an object file and then the next sh command creates an executable from object file i.e. helloWorld from helloWorld.o

Scons

Scons is a Python based language and is acronym for Software Construction.SConstruct files which are similar to Makefiles are easier to maintain.SCons also supports C/C++ and Java™ files, and as compared to make Scons needs a lot lesser code.

References