PyCharm + pytest

Hi
Lately I have been doing more and more anvil-app building in PyCharm and IDE for python. Some of the reasons are:

  • database view and editing
  • auto-complete
  • using lint (black) for code readability
  • type checking
  • pytest (framework for writing tests)
  • debugging with breakpoints
    and the latest reason:
  • GitHub auto-pilot which suggests code for you

In order to do the above though, I have had to make an extra framework in order to test the app offline.
So I

  • made mock classes of the anvil.works forms and functions
  • added functionality so that SQLite database can be used instead of anvil’s online one (needed for tests)
  • modifying some anvil-extras code so that I can use that seamlessly in tests

The result is that I can now run a test like that below, on my local computer:

    def test_create(self):
        """This method will test this CRUD functionality."""
        # test create begins here
        # start the database and login a user
        user = self.login_user()
        assert anvil.users.get_user()
        # open the home FORM
        form_home = Home()
        # click the button to create a new boat
        form_home.link_create_click()
        # get the create FORM in the column panel
        form_create = form_home.main_column_panel.get_components()[0]
        # add boat name and length in the UI
        form_create.text_box_name.text = random_name()
        form_create.text_box_length.text = random_number(size=2)
        # click the save button
        form_create.button_save_click()
        # test that the boat is in the database
        sail_boat_store = indexed_db.create_store('sail_boats')
        # the keys are the boat names. is the boat length saved? Let's check the browser indexed_db
        boat_name = form_create.text_box_name.text
        assert boat_name in sail_boat_store
        assert boat_name == sail_boat_store[boat_name]['name']
        assert float(form_create.text_box_length.text) == sail_boat_store[boat_name]['length']
        # is the boat in the database?
        boat_row = app_tables.sailboats.get(name=boat_name, user_email=user['email'])
        assert boat_row
        assert boat_name == boat_row.name
        assert float(form_create.text_box_length.text) == boat_row.length
        # test create ends here

If you are interested in seeing more, I made a self-contained repo that you can run the tests in. It is cached_lists_anvil_works.

This is only an example and doesn’t have the automated scripts. It was built using the framework:
anvil_works_design_test

Here is the anvil.works app. I built it to test for myself the browser storage indexed_DB using anvil-works.

You might want to look at the mock classes here
anvil.
On looking through this repo, you might assume that I have only added functionality to those class and functions that I have so far used in my apps. And you would be right.

You might like to take it and make it for your own, adding bits here and there so that you can run your own apps in tests.

7 Likes