Beeware App Testing
Are you developing a Beeware app and want to ensure its quality and functionality? Beeware app testing is essential to make sure it meets the expectations of your users. In this article, we will explore the best practices for testing Beeware apps.
What is Beeware App Testing?
Beeware app testing is the process of testing applications developed using the Beeware framework. It involves testing the functionality of the app, ensuring that it works seamlessly on different platforms and devices, and verifying that the app meets all the requirements of the end-users. Beeware app testing helps developers to identify and fix bugs and other issues before the app is released to the market.
Running the test suite
You may be pleased to know that your project already has a test suite in place! When you generated the project, two main directories were created: src and tests. The source directory contains the application code, while the tests directory includes the test suite. Within the tests folder, there is a file called test_app.py which contains the following content:
def test_first(): "An initial test for the app" assert 1 + 1 == 2
You have a Pytest test case, which is a set of code that can be executed to verify a specific behavior of your application. In this instance, the test is merely a placeholder and does not examine any aspect of your application. Nonetheless, this test can be performed.
You may execute this test suite using the –test option when running briefcase dev. As this is your first time performing tests, you will need to pass in the -r option to guarantee that the necessary test requirements are installed.
MacOS
(beeware-mrxenv) $ briefcase dev --test -r [helloworld] Installing requirements... ... Installing dev requirements... done [helloworld] Running test suite in dev environment... =========================================================================== ============================= test session starts ============================== platform darwin -- Python 3.11.0, pytest-7.2.0, pluggy-1.0.0 -- /Users/brutus/beeware-tutorial/beeware-venv/bin/python3.11 cachedir: /var/folders/b_/khqk71xd45d049kxc_59ltp80000gn/T/.pytest_cache rootdir: /Users/brutus plugins: anyio-3.6.2 collecting ... collected 1 item tests/test_app.py::test_first PASSED [100%] ============================== 1 passed in 0.01s ===============================
Linux
(beeware-mrxenv) $ briefcase dev --test -r [helloworld] Installing requirements... ... Installing dev requirements... done [helloworld] Running test suite in dev environment... =========================================================================== ============================= test session starts ============================== platform linux -- Python 3.11.0 pytest==7.2.0 py==1.11.0 pluggy==1.0.0 cachedir: /tmp/.pytest_cache rootdir: /home/brutus plugins: anyio-3.6.2 collecting ... collected 1 item tests/test_app.py::test_first PASSED [100%] ============================== 1 passed in 0.01s ===============================
Windows
(beeware-mrxenv) C:\...>briefcase dev --test -r [helloworld] Installing requirements... ... Installing dev requirements... done [helloworld] Running test suite in dev environment... =========================================================================== ============================= test session starts ============================== platform win32 -- Python 3.11.0 pytest==7.2.0 py==1.11.0 pluggy==1.0.0 cachedir: C:\Users\brutus\AppData\Local\Temp\.pytest_cache rootdir: C:\Users\brutus plugins: anyio-3.6.2 collecting ... collected 1 item tests/test_app.py::test_first PASSED [100%] ============================== 1 passed in 0.01s ===============================
Congratulations! You have successfully performed a single test that confirms Python math operates in the expected manner (what a relief!).
Now, let’s replace the current placeholder test with a new one that verifies the generate() method performs as anticipated. You may substitute the contents of test_app.py with the following code:
def add(a,b): return a+b def subtract(a,b): return a-b def test_1(): """Performs subtraction between two numbers""" assert add(1,2) == 3 def test_2(): """Performs addition between two numbers""" assert subtract(10,7) == 3
You can define two new tests to confirm the two expected behaviors: the result of adding two numbers and the result of subtracting two numbers. After defining these tests, you can run the test suite again without the -r option since the test requirements have already been installed. Instead, you just need to use the –test option.
MacOS
(beeware-mrxenv) $ briefcase dev --test [helloworld] Running test suite in dev environment... =========================================================================== ============================= test session starts ============================== ... collecting ... collected 2 items tests/test_app.py::test_name PASSED [ 50%] tests/test_app.py::test_empty PASSED [100%] ============================== 2 passed in 0.11s ===============================
Linux
(beeware-mrxenv) $ briefcase dev --test [helloworld] Running test suite in dev environment... =========================================================================== ============================= test session starts ============================== ... collecting ... collected 2 items tests/test_app.py::test_name PASSED [ 50%] tests/test_app.py::test_empty PASSED [100%] ============================== 2 passed in 0.11s ===============================
Windows
(beeware-mrxenv) C:\...>briefcase dev --test [helloworld] Running test suite in dev environment... =========================================================================== ============================= test session starts ============================== ... collecting ... collected 2 items tests/test_app.py::test_name PASSED [ 50%] tests/test_app.py::test_empty PASSED [100%] ============================== 2 passed in 0.11s ===============================
Test Driven Development
With the test suite in place, you can utilize it to guide the development of new functionalities.
To begin with, let’s add a custom greeting for a specific user in our app. We can start by appending a test case for the desired behavior at the end of test_app.py.
def test_3(): """If the name is BeeWare, a particular content is provided""" assert response("BeeWare") == "BeeWare is the latest Python framework!"
Next, execute the test suite using this newly added test.
macOS
(beeware-mrxenv) $ briefcase dev --test [helloworld] Running test suite in dev environment... =========================================================================== ============================= test session starts ============================= platform win32 -- Python 3.11.1, pytest-7.3.1, pluggy-1.0.0 -- C:\Users\sexy inayat\Desktop\Beeware\beeware-mrx_tutorial\beeware-mrxenv\Scripts\python.exe cachedir: C:\Users\harry\AppData\Local\Temp\.pytest_cache rootdir: C:\Users\harry\Desktop\Beeware\beeware-mrx_tutorial\helloworld plugins: anyio-3.6.2 collecting ... collected 3 items tests/test_app.py::test_1 PASSED [ 33%] tests/test_app.py::test_2 PASSED [ 66%] tests/test_app.py::test_3 FAILED [100%] ================================== FAILURES =================================== ___________________________________ test_3 ____________________________________ def test_3(): """If the name is BeeWare, a particular content is provided""" > assert response("BeeWare") == "BeeWare is the latest Python framework!" E AssertionError: assert 'BeeWare, welcome to BeeWare app development' == 'BeeWare is the latest Python framework!' E - BeeWare is the latest Python framework! E + BeeWare, welcome to BeeWare app development tests\test_app.py:24: AssertionError =========================== short test summary info =========================== FAILED tests/test_app.py::test_3 - AssertionError: assert 'BeeWare, welcome t... ========================= 1 failed, 2 passed in 0.97s =========================
Linux
(beeware-mrxenv) $ briefcase dev --test [helloworld] Running test suite in dev environment... =========================================================================== ============================= test session starts ============================= platform win32 -- Python 3.11.1, pytest-7.3.1, pluggy-1.0.0 -- C:\Users\sexy inayat\Desktop\Beeware\beeware-mrx_tutorial\beeware-mrxenv\Scripts\python.exe cachedir: C:\Users\harry\AppData\Local\Temp\.pytest_cache rootdir: C:\Users\harry\Desktop\Beeware\beeware-mrx_tutorial\helloworld plugins: anyio-3.6.2 collecting ... collected 3 items tests/test_app.py::test_1 PASSED [ 33%] tests/test_app.py::test_2 PASSED [ 66%] tests/test_app.py::test_3 FAILED [100%] ================================== FAILURES =================================== ___________________________________ test_3 ____________________________________ def test_3(): """If the name is BeeWare, a particular content is provided""" > assert response("BeeWare") == "BeeWare is the latest Python framework!" E AssertionError: assert 'BeeWare, welcome to BeeWare app development' == 'BeeWare is the latest Python framework!' E - BeeWare is the latest Python framework! E + BeeWare, welcome to BeeWare app development tests\test_app.py:24: AssertionError =========================== short test summary info =========================== FAILED tests/test_app.py::test_3 - AssertionError: assert 'BeeWare, welcome t... ========================= 1 failed, 2 passed in 0.97s =========================
Windows
(beeware-mrxenv) C:\...>briefcase dev --test [helloworld] Running test suite in dev environment... =========================================================================== ============================= test session starts ============================= platform win32 -- Python 3.11.1, pytest-7.3.1, pluggy-1.0.0 -- C:\Users\sexy inayat\Desktop\Beeware\beeware-mrx_tutorial\beeware-mrxenv\Scripts\python.exe cachedir: C:\Users\harry\AppData\Local\Temp\.pytest_cache rootdir: C:\Users\harry\Desktop\Beeware\beeware-mrx_tutorial\helloworld plugins: anyio-3.6.2 collecting ... collected 3 items tests/test_app.py::test_1 PASSED [ 33%] tests/test_app.py::test_2 PASSED [ 66%] tests/test_app.py::test_3 FAILED [100%] ================================== FAILURES =================================== ___________________________________ test_3 ____________________________________ def test_3(): """If the name is BeeWare, a particular content is provided""" > assert response("BeeWare") == "BeeWare is the latest Python framework!" E AssertionError: assert 'BeeWare, welcome to BeeWare app development' == 'BeeWare is the latest Python framework!' E - BeeWare is the latest Python framework! E + BeeWare, welcome to BeeWare app development tests\test_app.py:24: AssertionError =========================== short test summary info =========================== FAILED tests/test_app.py::test_3 - AssertionError: assert 'BeeWare, welcome t... ========================= 1 failed, 2 passed in 0.97s =========================
You have encountered a test failure, and the reason behind it has been provided in the output. The test is anticipating the output “BeeWare is the latest Python framework!”, but your response() implementation is producing “BeeWare, welcome to BeeWare app development”. To resolve this, you need to make changes to the response() function in the src/helloworld/app.py file, to update its behavior accordingly:
def response(name): if name: if name == "BeeWare": return "BeeWare is the latest Python framework!" else: return f"{name}, welcome to BeeWare app development" else: return "Hey, Anonymous"
Upon rerunning the tests, we will observe that our tests are now successful and pass without any errors.
MacOS
(beeware-mrxenv) $ briefcase dev --test [helloworld] Running test suite in dev environment... =========================================================================== ============================= test session starts ============================= platform win32 -- Python 3.11.1, pytest-7.3.1, pluggy-1.0.0 -- C:\Users\sexy inayat\Desktop\Beeware\beeware-mrx_tutorial\beeware-mrxenv\Scripts\python.exe cachedir: C:\Users\harry\AppData\Local\Temp\.pytest_cache rootdir: C:\Users\harry\Desktop\Beeware\beeware-mrx_tutorial\helloworld plugins: anyio-3.6.2 collecting ... collected 3 items tests/test_app.py::test_1 PASSED [ 33%] tests/test_app.py::test_2 PASSED [ 66%] tests/test_app.py::test_3 PASSED [100%] ============================== 3 passed in 0.32s ==============================
Linux
(beeware-mrxenv) $ briefcase dev --test [helloworld] Running test suite in dev environment... =========================================================================== ============================= test session starts ============================= platform win32 -- Python 3.11.1, pytest-7.3.1, pluggy-1.0.0 -- C:\Users\sexy inayat\Desktop\Beeware\beeware-mrx_tutorial\beeware-mrxenv\Scripts\python.exe cachedir: C:\Users\harry\AppData\Local\Temp\.pytest_cache rootdir: C:\Users\harry\Desktop\Beeware\beeware-mrx_tutorial\helloworld plugins: anyio-3.6.2 collecting ... collected 3 items tests/test_app.py::test_1 PASSED [ 33%] tests/test_app.py::test_2 PASSED [ 66%] tests/test_app.py::test_3 PASSED [100%] ============================== 3 passed in 0.32s ==============================
Windows
(beeware-mrxenv) C:\...>briefcase dev --test [helloworld] Running test suite in dev environment... =========================================================================== ============================= test session starts ============================= platform win32 -- Python 3.11.1, pytest-7.3.1, pluggy-1.0.0 -- C:\Users\sexy inayat\Desktop\Beeware\beeware-mrx_tutorial\beeware-mrxenv\Scripts\python.exe cachedir: C:\Users\harry\AppData\Local\Temp\.pytest_cache rootdir: C:\Users\harry\Desktop\Beeware\beeware-mrx_tutorial\helloworld plugins: anyio-3.6.2 collecting ... collected 3 items tests/test_app.py::test_1 PASSED [ 33%] tests/test_app.py::test_2 PASSED [ 66%] tests/test_app.py::test_3 PASSED [100%] ============================== 3 passed in 0.32s ==============================
Runtime Tests
Up until now, you have been executing the tests in development mode which is particularly advantageous while you’re working on new features. It allows you to quickly add tests and corresponding code to pass them. Nonetheless, there will come a time when you would like to ensure that your code functions correctly within the bundled application environment as well.
You can pass the –test and -r options to the run command. By running briefcase run –test -r, the identical test suite will execute within the packaged application bundle rather than your development environment.
macOS
(beeware-mrxenv) $ briefcase run --test -r [helloworld] Updating application code... Installing src/helloworld... done Installing tests... done [helloworld] Updating requirements... ... [helloworld] Built build/helloworld/macos/app/Hello World.app (test mode) [helloworld] Starting test suite... =========================================================================== Configuring isolated Python... Pre-initializing Python runtime... PythonHome: /Users/brutus/beeware-tutorial/helloworld/macOS/app/Hello World/Hello World.app/Contents/Resources/support/python-stdlib PYTHONPATH: - /Users/brutus/beeware-tutorial/helloworld/macOS/app/Hello World/Hello World.app/Contents/Resources/support/python311.zip - /Users/brutus/beeware-tutorial/helloworld/macOS/app/Hello World/Hello World.app/Contents/Resources/support/python-stdlib - /Users/brutus/beeware-tutorial/helloworld/macOS/app/Hello World/Hello World.app/Contents/Resources/support/python-stdlib/lib-dynload - /Users/brutus/beeware-tutorial/helloworld/macOS/app/Hello World/Hello World.app/Contents/Resources/app_packages - /Users/brutus/beeware-tutorial/helloworld/macOS/app/Hello World/Hello World.app/Contents/Resources/app Configure argc/argv... Initializing Python runtime... Installing Python NSLog handler... Running app module: tests.helloworld --------------------------------------------------------------------------- ============================= test session starts ============================== ... collecting ... collected 3 items tests/test_app.py::test_1 PASSED [ 33%] tests/test_app.py::test_2 PASSED [ 66%] tests/test_app.py::test_3 PASSED [100%] ============================== 3 passed in 0.21s =============================== [helloworld] Test suite passed!
Linux
(beeware-mrxenv) $ briefcase run --test -r [helloworld] Finalizing application configuration... Targeting ubuntu:jammy (Vendor base debian) Determining glibc version... done Targeting glibc 2.35 Targeting Python3.10 [helloworld] Updating application code... Installing src/helloworld... done Installing tests... done [helloworld] Updating requirements... ... [helloworld] Built build/helloworld/linux/ubuntu/jammy/helloworld-0.0.1/usr/bin/helloworld (test mode) [helloworld] Starting test suite... =========================================================================== ============================= test session starts ============================== ... collecting ... collected 3 items tests/test_app.py::test_1 PASSED [ 33%] tests/test_app.py::test_2 PASSED [ 66%] tests/test_app.py::test_3PASSED [100%] ============================== 3 passed in 0.21s ===============================
Windows
(beeware-mrxenv) C:\...>briefcase run --test -r [helloworld] Updating application code... Installing src/helloworld... done Installing tests... done [helloworld] Updating requirements... ... [helloworld] Built build\helloworld\windows\app\src\Hello World.exe (test mode) =========================================================================== Log started: 2022-12-02 10:57:34Z PreInitializing Python runtime... PythonHome: C:\Users\brutus\beeware-tutorial\helloworld\windows\app\Hello World\src PYTHONPATH: - C:\Users\brutus\beeware-tutorial\helloworld\windows\app\Hello World\src\python311.zip - C:\Users\brutus\beeware-tutorial\helloworld\windows\app\Hello World\src - C:\Users\brutus\beeware-tutorial\helloworld\windows\app\Hello World\src\app_packages - C:\Users\brutus\beeware-tutorial\helloworld\windows\app\Hello World\src\app Configure argc/argv... Initializing Python runtime... Running app module: tests.helloworld --------------------------------------------------------------------------- ============================= test session starts ============================== ... collecting ... collected 3 items tests/test_app.py::test_1 PASSED [ 33%] tests/test_app.py::test_2 PASSED [ 66%] tests/test_app.py::test_3 PASSED [100%] ============================== 3 passed in 0.21s ===============================
Similar to briefcase dev –test, you only require the -r option for the initial execution of the test suite to ensure that the test dependencies are installed. You can exclude this option for all subsequent test runs.
Furthermore, you can employ the –test option on mobile backends as well. Therefore, executing briefcase run iOS –test or briefcase run android –test will function correctly, allowing you to run the test suite on the selected mobile device.
Best Practices for Beeware App Testing
To ensure that the Beeware app testing process is effective, there are several best practices that developers should follow. These include:
- Creating a test plan that outlines the testing strategy and approach.
- Writing test cases that cover all the features of the app.
- Using automated testing tools to save time and effort.
- Testing the app on different platforms and devices to ensure compatibility.
- Prioritizing testing based on the risk of the feature or function.
Benefits
- Beeware allows developers to build apps that are compatible with multiple platforms such as iOS, Android, Windows, and Linux. This means that you can easily test your app on different platforms without the need for additional tools or resources.
- Beeware’s tools such as Toga and Briefcase allow developers to write code once and use it across multiple platforms, which reduces development time significantly. This means that developers can build and test their apps faster, which can save them time and money.
- Beeware’s toolset includes tools such as Batavia and BeeWare Test, which simplify the testing process for developers. These tools help to identify and fix bugs quickly, which can save developers a lot of time and effort.
- Beeware is an open-source project with an active community of developers who contribute to its development. This means that developers can get help and support from the community, as well as access to a range of resources and tools that can help them build better apps.
- Beeware’s tools are user-friendly and easy to use, even for developers who are new to the platform. The platform provides a range of documentation, tutorials, and examples that can help developers get started quickly.
- Beeware integrates with popular Integrated Development Environments (IDEs) such as Visual Studio Code and PyCharm, which makes it easy for developers to use their preferred tools.
Conclusion
The Beeware test suite is a valuable tool that can help you ensure the quality, reliability, and functionality of your code. With features such as automated testing, cross-platform compatibility, integration with other tools, a user-friendly interface, and code coverage analysis, the Beeware test suite can help you catch issues early, streamline your development workflow, and ultimately create high-quality Python applications. Overall, the Beeware test suite is an essential tool for any developer who wants to develop reliable and functional Python applications with ease.