0.01
A long-lived project that still receives updates
mrubyc-test is an unit test framework for mruby/c, supporting basic assertions, stub and mock.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 13
~> 3.9

Runtime

~> 0.12
~> 1.2
 Project Readme

mrubyc-test

Build Status

mrubyc-test is an unit test framework for mruby/c, supporting basic assertions, stub and mock.

Dependency

  • mrubyc-test 0.7.0+ depends on mrubyc 3.0+

Features

  • Tests are applicable to class and its instance methods written with mruby
  • C code will not be covered directly though, you can test your C implementation if you write mruby wrapper class. In this case, your test class (it also written with mruby) will test an integrated circumstance of C and mruby
  • Tests will run on your PC (POSIX) hereby you can write business logic with mruby/c apart from C API matters like microcontroller peripherals
  • Simple assertions ... enough for almost firmware development though, I will increase the number of assertion
  • Stub ... You can write your mruby code without peripheral implementation by C
  • Mock ... You can call any method still doesn't exist
  • The implementation of your application and test code will be analyzed by CRuby program, then compiled into mruby byte code and executed on mruby/c VM

Installation

Add this line to your application's Gemfile:

gem 'mrubyc-test'

And then execute:

$ bundle

Or install it yourself as:

$ gem install mrubyc-test

Usage

Assuming you are using mrubyc-utils to manage your project and rbenv to manage Ruby versions. It means you have Mrubycfile and .ruby-version in the top directory of your project.

Besides, you have to locate mruby model files that are the target of testing like mrblib/models/class_name.rb

And read here about why you should use mrubyc-utils.

This is an example of ESP32 project:

~/your_project $ tree
.
├── Mrubycfile                # Created by mrubyc-utils
├── .ruby-version             # It should be mruby's version like 'mruby-3.0.0'
├── Makefile
├── build
├── components
├── main
├── mrblib
│      └── models               # Place your model class files here
│            ├── class_name.rb  # The testing target `ClassName`
│            └── my_class.rb    # The testing target `MyClass`
│      └── loops
│            ├── main.rb        # Loop script isn't covered by mrubyc-test. use mrubyc-debugger
│            └── sub.rb         # Loop script isn't covered by mrubyc-test. use mrubyc-debugger
└── sdkconfig

In the same directory:

$ mrubyc-utils test init

Then, some directories and files will be created in your project. Now you can run test because a sample test code was also created.

$ mrubyc-utils test

You should get some assertion failures. Take a look at test/sample_test.rb to handle the failures and find how to write your own test.

Assertions

def assertions
  my_var = 1
  assert_equal     1, my_var  # => success
  assert_not_equal 2, my_var  # => success
  assert_not_nil   my_var     # => success
end

Stubs

Assuming you have a model file at mrblib/models/sample.rb

class Sample
  attr_accessor :result
  def do_something(arg)
    @result = arg + still_not_defined_method
  end
end

Then you can test #do_something method without having #still_not_defind_method like this:

def stub_case
  sample_obj = Sample.new
  stub(sample_obj).still_not_defined_method { ", so we are nice" }
  sample_obj.do_something("Ruby is nice")
  assert_equal 'Ruby is nice, so we are nice', sample_obj.result
end

Mocks

mrblib/models/sample.rb looks like this time:

class Sample
  def do_other_thing
    is_to_be_hit(1, 2) # Two args for example
  end
end

You can test whether #is_to_be_hit(v1, v2) method will be called:

def mock_case
  sample_obj = Sample.new
  mock(sample_obj).is_to_be_hit(2) # `2` should be count of parameters
  sample_obj.do_other_thing
end

Known problems

  • You have to write stub or mock test for all the methods still do not exist otherwise your test won't turn green

TODO (possibly)

  • Assertion against arguments of mock
  • Other assertions like LT(<), GTE(>=), include?, ...etc.
  • bla bla bla

Acknowledgements

The API design and implementation of this gem is greatly inspired by test-unit. Thank the great work.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/mrubyc/mrubyc-test. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

License

The gem is available as open source under the terms of the The 3-Clause BSD License.

Code of Conduct

Everyone interacting in the Mrubyc::Test project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.