This is a pre-production deployment of Warehouse, however changes made here WILL affect the production instance of PyPI.
Latest Version Dependencies status unknown Test status unknown Test coverage unknown
Project Description

Test more, cry less!

This script is meant to launch a folder of your tests which will behave as one big test suite. It’s done this way because of necessity having a fresh \(python\) interpreter for each Kivy application test to run without mistakes (otherwise mess from previous App().run() interferes).

Each unittest file in a folder consisting of tests must start with \(test_\) prefix and end with \(.py\)

Run from console:

python -m kivyunittest --folder "FOLDER"

Without --folder flag the file assumes it’s placed into a folder full of tests presumably as __init__.py. It makes a list of files, filters everything not starting with \(test_\) and ending with \(.py\) and runs each test.

Errors

If there is an error of whatever kind that unittest recognizes as failure, KivyUnitTest will save the name of the test and its log. When the testing ends all error logs are put together into console divided by pretty headers with test’s name.

Writing Unit Test for Kivy application

When the Kivy application starts, it creates a loop and until the loop is there, nothing will execute after App().run() line. That’s why we need to probe the loop.

This can be achieved by a simple time.sleep() as you’ve surely noticed sooner when trying to pause the app for a while. That’s exactly what a custom unittest for Kivy does - pauses the main loop as much as possible as a scheduled interval and executes the testing run_test function.

Example:

import unittest

import os
import sys
import time
import os.path as op
from functools import partial
from kivy.clock import Clock

First we need to set up importing of the application set main_path to be the folder of \(main.py\) e.g. when you have tests in \(<app dir>/tests/test_example.py\).

main_path = op.dirname(op.dirname(op.abspath(__file__)))
sys.path.append(main_path)

Import your main class that inherits from App (class My(App):) or even additional stuff that’s not connected with App class or its children.

from main import My


class Test(unittest.TestCase):
    # sleep function that catches `dt` from Clock
    def pause(*args):
        time.sleep(0.000001)

    # main test function
    def run_test(self, app, *args):
        Clock.schedule_interval(self.pause, 0.000001)

        # Do something

        # Comment out if you are editing the test, it'll leave the
        # Window opened.
        app.stop()

Create an instance of your application, put it as a parameter into partial (so that you could access it later), schedule main function with Clock and launch the application (working Window will appear).

    # same named function as the filename(!)
    def test_example(self):
        app = My()
        p = partial(self.run_test, app)
        Clock.schedule_once(p, 0.000001)
        app.run()

if __name__ == '__main__':
    unittest.main()

Tips for testing

Handle class communication through App class via App.get_running_app() in your application, put every needed widget inside App class like this:

class MyButton(Button):
    def __init__(self, **kwargs):
        super(<class name>, self).__init__(**kwargs)
        self.text = 'Hello Test'
        app = App.get_running_app()
        app.my_button = self

and then access your widgets in test’s run_test() function via app parameter like this:

self.assertEqual('Hello Test', app.my_button.text)

Use app.root to get instance of a class you pass in the build() function in the App class.

Dispatch events through widgets e.g. <widget>.dispatch('on_release') to execute function bound to \(on_release\).

Use Kivy’s Inspector module as help to navigate down the path of App class and use \(ids\) in \(kv language\), it’ll make targeting a specific widget easier.

Try even Kivy’s Recorder module to record steps and play them later instead of dispatching events manually. However, this way is heavy time-consuming as it plays the steps exactly as long as they were recorded.

Example:

from kivy.input.recorder import Recorder

# place this inside ``run_test()``
rec = Recorder(filename='myrecorder.kvi')
rec.bind(on_stop=<function>)
rec.play = True

This will play all steps and then executes a function bound to on_stop. May be useful for testing touch gestures, swipes, dragging and other rather annoying to write manually stuff.

There’s also possibility to change time the steps were recorded in in \(.kvi\) file (that long number), which will speed things up.

License

The MIT License (MIT)

Release History

Release History

0.1.3

This version

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.1.2

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.1.1

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.1.0

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

Download Files

Download Files

TODO: Brief introduction on what you do with files - including link to relevant help section.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
kivyunittest-0.1.3.zip (7.9 kB) Copy SHA256 Checksum SHA256 Source Oct 28, 2016

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting