Resources

Creating tests for Shiny for Python apps | Karan Gathani | Posit

With the Shiny for Python v1.0 release, Shiny provides a simple way to create a test file for your Shiny app. The shiny add test command is a helpful CLI tool for Shiny app developers. It simplifies the process of creating test files for their applications. When you run this command, it prompts you to input two pieces of information: the path to your Shiny app file and the desired location for the new test file. Once you provide these details, the command automatically generates a test file at the specified location. This new file includes a pre-made test template, giving you a solid starting point for writing your app's tests. Reference documentation - https://shiny.posit.co/py/docs/unit-testing.html https://shiny.posit.co/py/docs/playwright-testing.html

image: thumbnail.jpg

Transcript#

This transcript was generated automatically and may contain errors.

Hi everyone, I'm Karan. I work as a QA engineer in the Shiny team and for today's video I'll be talking about how you can leverage the testing module within Shiny to actually create your test and we have a bunch of really cool utility functions you could use to actually write your tests and so I'm hoping to actually show you. So I'll break this down into two videos. The first one we'll actually just see how you can run or create a test file for your Shiny apps.

The second one I'll explain more about how you would actually end up leveraging some of our controllers that allow you to actually customize what you want to test and how you want to test it. So I have an empty directory right now. This is just the virtual environment and one of the first things that you would want to do is before you can even write the test or leverage it is make sure you have PyTest Playwright installed. So you'll do pip install pytest and I have that so you make sure you install it. I have it installed but that is something that you'll need the Playwright and the PyTest framework to actually run a test.

Creating a test file with shiny add test

And so for this one let me actually use a basic Shiny app to actually show how we can create tests for that. So for that what I'm going to do is I'm going to use the Shiny create utility. So if I do Shiny create it'll ask me what kind of app I want and I will select a basic app which should be very straightforward. It'll ask me if I want to use Shiny Express. I'll be like yeah. And then for the destination directory I'll be like yeah just give me at the same directory I'm at. So if you see we have this basic app and I can actually run this app so you kind of know what this app is about. So there's a slider in this app and then this output text box that you can actually see. So the slider in a way just while you end up changing it to the output text box just doubles it. So you can see you can double it to 18.

Okay cool. So in order for us to actually write the test for these ones I'm going to close this one since we don't need the Shiny app right now.

So with the release of 1.0 and Shiny what we do is we expose a command in your terminal you can use and it's called Shiny add test and that's what we are going to do. So we'll do Shiny add test and so what it'll do is it'll ask you like hey can you give me the path to your app file. So the app file is the Shiny app that you want to add test for. So I can actually select basic app and you need the path through the entire like the app.py not the directory the app.py. So I'll select that and then I'll be app.py and then it'll ask me like all right can you tell me the path where you want the test file and then by default will give a name like test underscore app. You can always customize it to however you want but by default it will actually create a directory called test within the location where you had the app and then it'll create the test.

So I'll actually create two different versions one where the tests are living in the test directory and the one where the tests are alongside the app and I'll show you both the little different just different flavors but it should work pretty seamlessly for most folks.

little different just different flavors but it should work pretty seamlessly for most folks.

Okay so I add it and so now you'll see that it created a test directory and there's a test app. It doesn't have a lot of stuff it actually has a lot of inputs that you would need for your app but then it still needs you to write that. So I'll go more over about how we'll write the test and stuff like that in the second video but for now I'll just go over how you can run the tests.

Two flavors of test file placement

Okay so this is the one when you have it within the test folder and then for the second one what I'll do is I'll actually run invoke the shiny add test command again and it'll ask me like all right give me the so it'll by default always will do will default to like app.py which in the same directory but in this case we have it put it under the basic app directory so I'll be like all right app.py this is my shiny app and then I'll be like okay for the path to the test file what I'll do is in this case I will actually do change the name a local app but this one you can see it okay just did not refresh in vs code so you can see now we have this one which is lying just besides the shiny app so you have two flavors so we have one where you have a distinct folder called test and it's so when you actually look at the code for them they are a little bit different

for the test app though when we so we use a fixture which allows you to actually start the shiny app when you invoke pytest and so for this one what we'll do is we'll actually just copy the path to the shiny app so this is the path so this is like relative to the directory you are going to trigger the pytest from so we have that and we have this so just for the simplicity right now because I'm not going to go in too much detail about these tests or the different things that we exposed to do the test I'm just going to copy some code that I'll actually go over in the next video so what I'll do is I'll actually copy some test code over here

and what this does is essentially it verifies that the slider value but the default one is 20 and so the double of that like in the text box is n times 2 is 40 and I'm going to do that for both of them so once you have that I'll go more over about what this is doing in the next video but right now for me to invoke the test I'll just do pytest and it should actually automatically run the test and it says both of them passed in case if I actually have an error in one of the tests when by default when the app loads it's always the slider values are 20 so the double is 40 but in this one I will actually deliberately make the test fail to just show you what kind of error you will get and so in this case when it will fail which is this one it'll say it'll fail saying that it expected to be n times 2 is 30 and then when you actually see it says that it actually you expected it to be 30 but it found 40 so this is how it would look like when it's failing and you just get those two dots if it's passing all right so that's the end of the video one if you want to learn more about how to get started with your testing I would ask you to refer to the getting started with shiny there's a testing section that should have all the details of how to um run pytest install all the packages that you need for that and set up your first test