CodeaUnit is a unit testing framework for Codea on iPad. I understand that many people may think of Codea as a playground to simply hack together ideas and perhaps testing doesn't matter to them. I personally feel test-driving code can get you to your desired goal faster and more efficiently.
Codea is a fantastic tool for coders and CodeaUnit is intended to provide means of proper unit testing for it.
-
Clear out the content in the 'Main' tab (it isn't needed).
-
Create a new class/tab called 'CodeaUnit' and paste in the contents of
CodeaUnit.lua
. -
Add your new CodeaUnit project as a dependency of project that you want to make testable.
When you add the CodeaUnit dependency to your project you get a couple of things.
-
A global instance of the CodeaUnit framework will be available via the
_
(underscore) variable. -
When you run your code you will see a button titled 'CodeaUnit Runner' in the parameters view.
CodeaUnit provides a runner which is currently fairly primitive but is still pretty convenient. Simply hit the 'CodeaUnit Runner' button (or CodeaUnit.execute()
programmatically). The runner will look for any global functions in your entire application (all classes) whose names start with 'test' and it will execute them for you. For cleanliness, a best practice will be to put your test functions in test classes but location really doesn't matter.
The runner is optional. You can also fire your tests off inline anywhere you want by calling the describe
function. More detail in the example below.
is()
isnt()
has()
(check if array/table contains item)throws()
-- The runner would find and execute this method due to its name starting with 'test'
function testCodeaUnitFunctionality()
-- This option changes verbosity of the output. Defaults to true.
-- only feature names, failed tests, and the summary will show if set to false.
CodeaUnit.detailed = false
-- Nested describe calls are not currently supported.
-- However, multiple can of course be used next to eachother.
-- describe("feature name", callback)
_:describe("CodeaUnit Test Suite", function()
-- Executed before each test
_:before(function()
-- Some setup
end)
-- Executed after each test
_:after(function()
-- Some teardown
end)
-- The callback doesn't have to be inline like this
-- can also be a reference to the function which is declared elsewhere
_:test("Equality test", function()
_:expect("Foo").is("Foo")
end)
_:test("Negation test", function()
_:expect("Bar").isnt("Foo")
end)
_:test("Containment test", function()
_:expect({"Foo", "Bar", "Baz"}).has("Foo")
end)
_:test("Thrown test", function()
-- When using the 'throws' expectation, you need to pass in a function
-- reference to the 'expect' method.
_:expect(function()
error("Foo error")
end).throws("Foo error")
end)
-- Use ignore instead of test if you want to temporarily skip this execution
_:ignore("Ignored test", function()
_:expect("Foo").is("Foo")
end)
_:test("Failing test", function()
_:expect("Foo").is("Bar")
end)
end)
end