Project

liveunit

0.0
No commit activity in last 3 years
No release in over 3 years
Example implementation to test my experiment on live unit testing. Make sure to check the blog post here : http://zisismaras.me/general/2015/05/01/exploring-live-unit-tests.html and the github repo.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 3.2.0, ~> 3.2

Runtime

~> 0.3.1
>= 5.7.0, ~> 5.7
 Project Readme

LiveUnit

Example implementation to test my experiment on live unit testing.
Check the blog post here.

##Installing

gem install liveunit

##Using Include the module and place the evaluate_me method call in the entry point of your object(eg. the initialize method)

require 'liveunit'

class Example
  include LiveUnit
  def initialize
    evaluate_me #!
    @myvar = 15
  end
  def calculate(num)
    num + 10
  end
end

ex = Example.new
puts ex.calculate(40)

Then let's create a test for our calculate method in a livetest/test_example.rb file:

require 'liveunit/testcase'

class TestExample < LiveUnit::TestCase
  #doomed to fail
  def test_calculate
    msg "Result should be greater than 400 when @myvar==15"
    assert_equal(15, state[:@myvar])
    assert_operator myreturn, :>=, 400
  end
end

Run the program and you should see this :

Failure : TestExample#test_calculate
Message : Result should be greater than 400 when @myvar==15
Expected 50 to be >= 400.

Features/Problems

The minitest's assertion system is used so most assert operations should work.
Autoloading and discovering is quite dumb so certain conventions must be followed:

  1. Test files should be placed in a /livetest folder in the program's root directory.
  2. Test file name should be in a format of test_objectname.rb.
  3. Unit test suite has to be in a format of class TestObjectName < LiveUnit::TestCase
  4. Unit test method should be named def test_method_name

It has not been tested with more complex things like rack applications or anything that actually matters, only simple plain ruby programs.
Runtime overhead is significant.

Creating custom reporters

By default a reporter that writes to stdout will be used but it can be easily changed.
Let's create a reporter that writes to a log file, just subclass LiveUnit::Reporter and overwrite the report method. The report method gets executed after each test and the results are available in the results method.

require 'logger'
require 'liveunit/reporter'

class MyReporter < LiveUnit::Reporter
  def initialize
    @logger = Logger.new('logs')
    super
  end

  def report
    results.each do |re|
      @logger.error("Case : #{re[:case]} Failed.")
      @logger.error("Message : #{re[:msg]}")
      @logger.error("Enviroment : #{re[:env]}")
      @logger.error("Expectation : #{re[:expectation]}")
    end
  end
end

then just pass it to the evaluate_me method : evaluate_me(MyReporter)

Final Notice

Make sure you also read the blog post if you are interested.
This is not meant to be used in anything serious(it can't anyway)
It is just an experimental testing tool and will be probably die soon.