test a micropython project?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
jedie
Posts: 252
Joined: Fri Jan 29, 2016 12:32 pm
Contact:

test a micropython project?

Post by jedie » Tue Dec 10, 2019 5:38 pm

I have some tests and run flake8 in https://github.com/jedie/micropython-sonoff-webswitch
This is also the first project used github actions as CI.

But the existing tests runs only with CPython.
Somebody tried to run tests with micropython using the Unix port in a CI pipeline?

jedie
Posts: 252
Joined: Fri Jan 29, 2016 12:32 pm
Contact:

Re: test a micropython project?

Post by jedie » Tue Dec 10, 2019 8:32 pm

Maybe one way is to implement MicroPython-specific modules as mocks ? I started with this a little bit: https://github.com/jedie/micropython-so ... 8e569f8398

But maybe there already exists a full features mock project???

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: test a micropython project?

Post by stijn » Wed Dec 11, 2019 9:24 am

The run-tests which comes with micropython itself is usable by CI builds (it gets used for all travis build for instance).

'Standard' unit tests for truly platform-indpendent code can be ran with CPython, for more micropython-specific code you could use unittest.py from the micropython-lib project and then run it either on the unix port or on an emulated or real target.

Irregardless of which method you use you can use mocks, but having to write a lot of those without having something like unittest.mock is tedious so whenever possible I prefer writing logic which can be tested easily without mocks, or else just run it on the target platform.

I don't think there's a working implementation of a mocking framework yet, but with functionality like __getattr__ etc being added to the core this should become possible.

jedie
Posts: 252
Joined: Fri Jan 29, 2016 12:32 pm
Contact:

Re: test a micropython project?

Post by jedie » Tue Jan 07, 2020 7:08 am

I now implement "integration" tests using asynctest and test the full uasyncio.start_server() stack :P
Tests can look like this:

Code: Select all

    async def test_non_well_form_request(self):
        response, server_message = await self.get_request(
            request_line=b"GET-totaly-bullshit-HTTP/1.1"
        )
        assert response == 'HTTP/1.0 303 Moved\r\nLocation: /main/menu/\r\n\r\n'
        assert server_message == 'not enough values to unpack (expected 3, got 1)'

    async def test_get_main_menu(self):
        response, server_message = await self.get_request(request_line=b"GET /main/menu/ HTTP/1.1")
        self.assert_response_parts(
            response,
            parts=(
                'HTTP/1.0 200 OK',
                '<html>',
                '<title>network name-04030201 - OFF</title>',
                '<p>Power switch state: <strong>OFF</strong></p>',
                '<p>Web server started...</p>',
                'RAM total: 1.95 KB, used: 0.98 KB, free: 0.98 KB<br>',
                'Server local time: 2000-01-01 00:00:00',
                '</html>'
            )
        )
        assert 'Web server started...' == server_message
See: https://github.com/jedie/micropython-so ... 50d6991df6

I implement only mocks that currently been used, stuff like e.g.: gc.mem_free(), machine.unique_id()
Think it makes sence to split all this from the project and create a "mpy-test" project... But unfortunately the day only has 24 hours.

Post Reply