Introduction
Pytest Assist (PA)
is an extension of pytest.
It provides a GUI in web browser, including the following functionalities:
- selecting tests to run by specifying markers, keywords or target files/classes/functions.
- realtime monitoring testing status/stats from local or remote.
- generating pretty reports.
The architecture is like this
This is a screenshot of PA.
Installation
Just run pip install pytest-assist
Quick Start
With GUI
To launch PA
, open terminal window, enter the root directory of your pytest project.
And enter the following command
python -m pytest_assist
Assist server
will be lauched, and a browser window will popup automatically.
If it is Windows you are using, that browser window will be in application mode
, that is, no tools bar or address bar.
You could also open browser to connect to assist server
, just enter the url like http://127.0.0.1:48530
If you are connecting to assist server
from remote, replace the IP with it’s IP address or hostname.
PA supports multiple browsers in the same time.
The default HTTP port of assist server
is 48530.
If you want to use different port, specify it with command line argument port
like
# specify IP , HTTP port
python -m pytest_assist --host 0.0.0.0 --port 10000
# specify websocket port
python -m pytest_assist --wsport 30000
# specify HTTP port and websocket port
python -m pytest_assist --port 10000 --wsport 30000
# check arguments with help switch
python -m pytest_assist --help
The GUI is like the following
Just click “Test Params Setting” to open settings and click Run Testing
to start a complete testing.
Report Only
If you just want PA to create report without GUI needed, just run pytest like this
python -m pytest -p pytest_assist.plugin --assist-report=myreport.html
Pick Tests
You could pick test cases in the following ways:
-
specify the keywords of nodeid of tests
In
Keyword Filters
input box, fill in the keyword and press enter key. Repeat that to add other keywords.Click the keywords on the right to cancel it.
-
specify the markers of tests
Just like add keywords above.
-
Choose target test module files/classes/functions(methods)
PA
display the test module files/classes/functions(methods) inTarget Tests
area.If those were changed, just click the refresh icon to update.
Double click one item (file/class/function) to add it as one test target.
Single click the test file to show the test classes/functions in that file.
The checkbox Save Result
is check by default, so the test result will be saved for later check or export.
The string in Test Name
is the name for this testing. If nothing specified, it will take the start time of testing as name.
Only alphabetic characters and -
_
are allowed.
Save Arguments Settings
You could save your arguments for later use.
Just specify a name and click Save Params
.
They will be saved are displayed in the Test params
list on the left column.
Double click the saved item to load it.
The params are saved in the file in directory .pytest_assist
of the current working dir, which should be the project root directory.
If you want to delete one saved item, just hold shift
and double click it.
Monitor Testing Status
When a testing started, it will show the ongoing status below
Overall stats and the details of every tests are displayed also.
The stats items with 0 count value are not displayed.
For example, if all tests passed, the errors/failed numbers are not displayed, just show passed number.
The details of one test includes :
-
Node-id of test
-
The details of 3 phases: setup, call and teardown.
There are 3 kinds of info:
-
Report info
They are generated by your test code by calling PA lib functions.
We will introduce them later.
-
Terminal output
Terminal output usually are genenrated by
print
functions of your test code.Those are captured and displayed in gray background color.
-
Error details
The error details generated by pytest will be displayed in light red background color.
-
If you tick off the checkbox Show details
, only nodeid of tests are displayed, so you could see them in bref.
The node-ids of failed tests are in red, passed are in green.
Those with setup/teardown errors are in purple.
You could click the nodeid stripe of one test to expand it or collapse it.
You could abort ongoing testing if necessary by click Abort Testing
button on the top
If current testing were busy in executing your time consuming code, and you could not wait any more, just click Force Aborting
. That is actually restart the Assist Server.
Click Shut Down
to shut the Assist Server down. It is userful when you are remotely control the server.
View/Export History Testing
Double click the items of History Records
on left to open a history testing report.
If you want delete one record, just hold shift
and double click it.
Click the Export
button to save the record as report to local.
It is a zip file with a report HTML file and maybe some screenshot images inside.
You could send it to others.
Output Into Report
We often want to write some information into the report to show what happend during testing, like this
The output of print
function can appear in the report, but we do not recommend that approach.
The print function is more suitable for outputting some temporary debugging information. When completed, better to comment them out.
PA provides some functions to generate colorful report.
They are log_str
, and some shotcut funcionts, like log_step
, log_str_red
, log_str_green
, log_str_blue
log_str
log_str
is most basic report function.
You could use it just like print
function, but with 2 extra argument color
and weight
to specify font color and weight.
They both have default value. Default value of color
is a lightly black color, and of weight
is normal
. You could specify any valid CSS value for them.
Like,
from pytest_assist import log_str
username = 'byhy'
def test_C009001():
log_str(1,'login with', username, color='green', weight='bold')
log_str(2, 'login successfully',color='green')
log_str('please noten when check manually', color='chocolate')
log_str('note1', 'blabla')
log_str('note2', end='') # without new line
log_str('-- blabla')
The output in report is like
shotcut functions
We often need to write step name in a test, log_step
is handy for that.
from pytest_assist import log_str, log_step
def test_C009001():
log_step(1, 'login with account: byhy')
log_str('some other info')
log_step(2, 'login successfully')
log_str('some other info')
The output in report is like
We also have 3 shotcut functions to output texts in red/green/blue.
They are log_str_red
, log_str_green
, log_str_blue
.
from pytest_assist import log_step, log_str_red, log_str_blue
def test_C009001():
log_str_red('red info')
log_str_green('green info')
log_str_blue('blue info')
You could actually define your own shotcut functions, like
def log_str_darkred(*args, sep=' ', end='\n', weight=None):
log_str(*args, sep=sep, end=end, color='darkred', weight=weight)
Screenshot
When you are writing automation test of web, and you want save screenshot, you could use PA lib function selenium_screenshot
.
from pytest_assist import selenium_screenshot, log_str
def test_C00888():
from selenium import webdriver
wd = webdriver.Chrome()
wd.get('https://www.your-website-under-testing')
log_str('before screenshot')
selenium_screenshot(wd) # Selenium Web driver object as argument
log_str('after screenshot')