How to run unit tests before building a ruby gem? - ruby

I am developing a gem and I have my unit tests in lib/spec directory. Currently I manually run unit tests using rspec lib/spec. And then I build gem using gem build mygem.gemspec (which builds a .gem file).
How can I run my unit tests along with gem build command? (Just like how we do production builds using npm)

If you have a build process that consists of multiple tasks (e.g. testing, creating a gem, publishing a gem), and has dependencies between those tasks (e.g. the gem should only be created if the tests are successful, in order to publish the gem, it needs to be created first), you can use a build tool to automate that.
Actually, build tools can be used for much more than just building, which is why some people prefer the term task-oriented programming or (my personal favority) dependency-oriented programming for that.
The most famous of such dependency-oriented programming tools is probably make. If you are familiar with the Java ecosystem, you probably know Apache Ant and Gradle. In the Microsoft world, there is the Microsoft Build Engine (MSBuild). From the ECMAScript ecosystem, you may know Grunt or Gulp. The hot new kid on the block is Google's Bazel.
Ruby also has its own share of such tools, the most widely-used one is Rake.
A lot of Ruby libraries and tools come with their own ready-made Rake Tasks, so that you don't have to write them yourself. For your particular use case, for example, there are ready-made tasks for building gems and for running RSpec tests.

Related

Automatically test when building gem

I don't like to run my tests every time I build my code, since the code itself is slow to run. However, I semi-frequently package it into a gem so I can send it to my co-workers. Is there a nice way (ideally within the .gemspec file) to automatically run my tests?
I've seen the test_files attribute in some people's gemspec code, is this intended for use by the installer?

In order to get my Gem published, do I absolutely need to write tests?

I would like to publish a Gem I wrote so it can be installed by users via gem install my-gem. The RubyGems guide on how to Make Your Own Gem has a section on using Test::Unit and rake to test my code.
Writing tests is going to add significant development time to a Gem that (in this case) is somewhat trivial in nature. I'd like to avoid it if possible.
Is it necessary to write tests in order to publish a Gem or just a suggestion?
It is a (good) suggestion, and is in no way required to publish a gem.

Confusing with ruby spec

Which is the difference among spec / rspec and mspec?
In ruby spec home, it said that we should use mspec command.
But some other guys said it should be 'spec kind_of_spec.rb'.
Also someones said we should use 'rspec'.
Which is the difference of these three modules?
In short - these modules have the same aim (testing) but different kinds of toolsets to reach that.
RubySpec . RubySpec is a project to write an executable specification for the Ruby Programming Language.
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.
MSpec - MSpec is a specialized framework that is syntax-compatible with RSpec for
basic things like +describe+, +it+ blocks and +before+, +after+ actions. MSpec
contains additional features that assist in writing the RubySpecs used by
multiple Ruby implementations.
These are different tools / project with very simliar names but completely different aims!
1) rspec
RSpec is a Tool you use to test your own ruby code. It's a
replacement for the built in testing framework of Rails described
in the Rails Guide here.
If you switch to rspec, you run the command
rake spec
to run your whole test suite, or you run a command like
rspec spec/models/course_spec.rb
to run one specific part of the test.
2) rubyspec and mspec
RubySpec is a project to write an executable specification for the Ruby Programming Language. There is a need for such a specification because there a several different ruby interpreters: Matz's Ruby Interpreter (called MRI) is the de facto standard, but there are also Rubinius, JRuby, IronRuby, MacRuby, HotRuby,...
MSpec is a tool used in developin the Ruby Specs. It's similar in usage zu rspec.
If you want to write a new Ruby interpreter you can use rubyspec to test if you
are doing it right.
But RubySpec is not something need if you're just using ruby to develop your own web project.
RubySpec isn't the same as RSpec
Starting with RSpec2 you should use rspec <path> command instead of rake spec to run your specs

How can I run Ruby tests automatically?

I have a system thats highly reliant on various web APIs. I would like to run my API specific tests at least once per day to make sure all API's are still playing nicely and alive. I have a set of unit tests (just plain rb files that test API calls for expected data) and would like to run these every 24 hours. If something breaks, I would like to take an action (e.g. email or sms me).
How best to setup automated Ruby tests and parse the result? Can I just setup a cron job to handle the .rb files? How would I take an action and detect programmatically if the tests are failing? Maybe there is some kind of continuous integration solution for RUby that can handle this?
I've just gone through the process of setting up Hudson CI as my integration server, using this amazing tutorial from Dr. Nic. It installs through a gem, coming pretty much preconfigured, and was extremely simple to get working.
I'm using rspec and cucumber, and Hudson runs all tests when it sees a new commit on my git repository. If all tests pass, it merges the code into my master branch. If any test fails, it holds its horses and sends me an email.
EDIT:
I also want to give ten thumbs up to the ChuckNorris plugin for Hudson. Agile doesn't get better than pair programming with Walker, Texas Ranger.
Ruby has Test::Unit built-in, RSpec, ZenTest, shoulda, cucumber and probably many more tools to help test. Being built-in, Test::Unit is used a lot and is the target to be beaten by the other tools.
ZenTest and RSpec can do continuous testing: You make a change and save a file and they'll see it and run the test suite. I like that because then I know the state of things right away.
I haven't used cucumber, but have used the rest. I heard cucumber's emphasis is on integration testing but that might have been the commenter's feelings rather than by design of the developers. The list of tutorials for cucumber is interesting browsing. In particular there's webrat: Automated Acceptance Testing with RSpec or Cucumber.
Any of these could be wired up with cron to run periodically; Just treat them as you would any other set of command-line apps.
It should be easy to tie in web-testing too, but you'll have to identify the gems/modules needed and write the glue code. I haven't had need for such a beast as I'd go at it using Mechanize and/or one of the other HTTP gems plus Nokogiri to ransack the pages.

What is the purpose of Rake?

I know Rake stands for Ruby Make, and I know Make is a unix build utility. Now, I come from working in Xcode building iPhone apps to Ruby, so I have never used Make before, and the only time I use rake is when in rails or installing some third party package and I type a command like rake db:migrate. The things I don't understand are ... What exactly is a build utility? What is the purpose of rake? What does it let me do? So if anyone can help answer any of these questions for me, it would be much appreciated.
Rake lets you script certain tasks on a per-project basis, much as a Makefile allows a Unix developer to script their compile and build process. The defined tasks you've used Rake with so far were included with the packages they came with (e.g. rake db:migrate comes with Rails, or at least with ActiveRecord) and automate certain tasks related to those packages (e.g. installing required gems for a Rails project).
If your project has certain tasks which are performed repeatedly, you can write a rake task to run those tasks which gets included in the SCM tree for the project and runs in the context of that project; in Rails they're in lib/tasks. You could write a Rake task to purge stale session records from your database, for example, and then set up a cron job to run it.

Resources