GithubHelp home page GithubHelp logo

st-testing-demo's Introduction

Streamlit App Testing Demo

Open in GitHub Codespaces

This repo demonstrates the capabilities and usage of Streamlit's new AppTest class, released in Streamlit 1.28. App testing enables developers to:

  • Run your app as a headless script
  • Inspect and make assertions about the output content via the DOM in object-attribute style
  • Programmatically modify the input values on various widgets, re-run the app, and inspect the output

AppTest works well with unittest.TestCase and pytest or similar tools. The testing framework ships with core Streamlit (and in fact is used heavily in Streamlit's internal unit testing) - the functionality shown here can be installed from the provided whl.

The example is built on the existing sophisticated_palette app originally built by @syasini and hosted on Streamlit Community Cloud: https://sophisticated-palette.streamlit.app/

A simple example

A simple example that demonstrates the anatomy of a unit test and the key features of the unit testing framework

import unittest
from streamlit.testing.v1 import AppTest

class TestSuite(unittest.TestCase):
    def test_sidebar(self):
        """Simple test of the sidebar controlling the palettes rendered"""

        # Load and run the script from a file path
        at = AppTest.from_file("app.py").run()

        # No exceptions were rendered in the app output
        assert not at.exception

        # Five color pickers are rendered on the first run
        assert len(at.color_picker) == 5

        # You can also query a widget by key to modify it or check the value
        assert at.number_input(key="sample_size").value == 500

        # Set the value of the first number input in the sidebar
        # (palette size) to 2, and re-run the app
        at.sidebar.number_input[0].set_value(2).run()

        # Two color pickers are rendered in the second run
        assert len(at.color_picker) == 2

Read the full documentation here.

See test_app.py for the tests and some further explanation.

Run it using GitHub Codespaces

You can get started very quickly by launching this repo in GitHub Codespaces. The app and Codespaces walkthrough should load automatically after a few seconds.

Run it locally

I recommend installing to a venv and then running the test suite with pytest.

git clone https://github.com/AnOctopus/st-testing-demo.git
cd st-testing-demo
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
pytest

Using it with CI

The testing framework just uses pytest, so any CI tools that work with python should just work. You can see a Github Action workflow in this repo that runs the tests using GitHub's python starter workflow with zero modifications, and it works great.

You can view the Github Actions configuration for this repository, and recent CI runs including the test execution.

๐ŸŽˆ Let us know what you think! ๐ŸŽˆ

st-testing-demo's People

Contributors

sfc-gh-jcarroll avatar syasini avatar anoctopus avatar

Stargazers

Alessia Marcolini avatar Gaspard Charles avatar Joviane Bellegarde avatar Betty M avatar Ceyda Cinarel (์žฌ์ด๋‹ค) avatar  avatar Cam Gorrie avatar Zachary Blackwood avatar Zachary Blackwood avatar Maciej Obarski avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.