SeleniumBase/help_docs/customizing_test_runs.md

131 lines
5.8 KiB
Markdown
Raw Normal View History

2018-08-01 02:47:10 +08:00
### Customizing test runs with **pytest** (or nosetests)
2017-10-27 04:43:33 +08:00
2018-08-01 02:47:10 +08:00
In addition to [settings.py](https://github.com/seleniumbase/SeleniumBase/blob/master/seleniumbase/config/settings.py) (which lets you customize SeleniumBase global properties) you can customize test runs from the command line:
2017-10-27 04:43:33 +08:00
2018-08-01 02:47:10 +08:00
* Choose the browser for tests to use (Default: Chrome)
2017-10-27 04:43:33 +08:00
* Choose betweeen pytest & nose unittest runners
* Choose whether to enter Debug Mode on failures
2018-08-01 02:47:10 +08:00
* Choose additional variables to pass into tests
* Change the automation speed (with Demo Mode)
* Choose whether to run tests multi-threaded
2017-10-27 04:43:33 +08:00
* Choose a Selenium Grid to connect to
2018-08-01 02:47:10 +08:00
* Choose a database to save results to
* Choose a proxy server to connect to
2017-10-27 04:43:33 +08:00
...and more!
2017-10-25 06:49:00 +08:00
2018-08-01 02:47:10 +08:00
#### **Examples:**
(These are run from the **[examples](https://github.com/seleniumbase/SeleniumBase/tree/master/examples)** folder.)
2017-10-25 06:49:00 +08:00
```bash
2018-02-16 07:21:04 +08:00
pytest my_first_test.py
2017-10-25 06:49:00 +08:00
2018-03-09 06:47:00 +08:00
pytest my_first_test.py --demo_mode --browser=chrome
2018-07-04 06:56:43 +08:00
pytest my_first_test.py --browser=firefox
2017-10-25 06:49:00 +08:00
2018-08-21 13:57:33 +08:00
pytest my_test_suite.py --html=report.html
2018-03-09 06:47:00 +08:00
nosetests my_test_suite.py --report --show_report
2018-08-01 02:47:10 +08:00
pytest my_test_suite.py --server=IP_ADDRESS -n 4
2018-03-09 06:47:00 +08:00
pytest my_test_suite.py --proxy=IP_ADDRESS:PORT
2018-08-21 13:57:33 +08:00
pytest test_fail.py -s --pdb --pdb-failures
2017-10-25 06:49:00 +08:00
```
2018-08-01 02:47:10 +08:00
You can interchange **pytest** with **nosetests**, but using pytest is strongly recommended because developers stopped supporting nosetests. Chrome is the default browser if not specified.
2017-10-25 06:49:00 +08:00
2018-02-16 07:21:04 +08:00
(NOTE: If you're using **pytest** for running tests outside of the SeleniumBase repo, **you'll want a copy of [pytest.ini](https://github.com/seleniumbase/SeleniumBase/blob/master/pytest.ini) at the base of the new folder structure**. If using **nosetests**, the same applies for [setup.cfg](https://github.com/seleniumbase/SeleniumBase/blob/master/setup.cfg).)
2017-10-25 06:49:00 +08:00
2018-08-01 02:47:10 +08:00
#### **Example tests using Logging:**
2017-10-25 06:49:00 +08:00
```bash
2018-03-05 06:55:00 +08:00
pytest my_test_suite.py --browser=chrome
2017-10-25 06:49:00 +08:00
```
2018-08-01 02:47:10 +08:00
(During test failures, logs and screenshots from the most recent test run will get saved to the ``latest_logs/`` folder. Those logs will get moved to ``archived_logs/`` if you have ARCHIVE_EXISTING_LOGS set to True in [settings.py](https://github.com/seleniumbase/SeleniumBase/blob/master/seleniumbase/config/settings.py), otherwise log files with be cleaned up at the start of the next test run.)
2017-10-25 06:49:00 +08:00
2018-08-01 02:47:10 +08:00
#### **Demo Mode:**
2017-10-25 06:49:00 +08:00
2018-08-01 02:47:10 +08:00
If any test is moving too fast for your eyes to see what's going on, you can run it in **Demo Mode** by adding ``--demo_mode`` on the command line, which pauses the browser briefly between actions, highlights page elements being acted on, and lets you know what test assertions are happening in real time:
2017-10-25 06:49:00 +08:00
```bash
2018-07-04 06:56:43 +08:00
pytest my_first_test.py --browser=chrome --demo_mode
2017-10-25 06:49:00 +08:00
```
You can override the default wait time by either updating [settings.py](https://github.com/seleniumbase/SeleniumBase/blob/master/seleniumbase/config/settings.py) or by using ``--demo_sleep={NUM}`` when using Demo Mode. (NOTE: If you use ``--demo_sleep={NUM}`` without using ``--demo_mode``, nothing will happen.)
```bash
2018-07-04 06:56:43 +08:00
pytest my_first_test.py --browser=chrome --demo_mode --demo_sleep=1.2
2017-10-25 06:49:00 +08:00
```
2018-08-01 02:47:10 +08:00
#### **Passing additional data to tests:**
If you want to pass additional data from the command line to your tests, you can use ``--data=STRING``. Now inside your tests, you can use ``self.data`` to access that.
#### **Running tests multithreaded:**
To run Pytest multithreaded on multiple CPUs at the same time, add ``-n=NUM`` or ``-n NUM`` on the command line, where NUM is the number of CPUs you want to use.
#### **Debugging tests:**
**You can use the following code snippets in your scripts to help you debug issues:**
2017-10-25 06:49:00 +08:00
```python
import time; time.sleep(5) # sleep for 5 seconds (add this after the line you want to pause on)
import ipdb; ipdb.set_trace() # waits for your command. n = next line of current method, c = continue, s = step / next executed line (will jump)
2018-08-01 02:47:10 +08:00
import pytest; pytest.set_trace() # similar to ipdb, but specific to pytest
2017-10-25 06:49:00 +08:00
```
2018-08-01 02:47:10 +08:00
**To pause an active test that throws an exception or error, add ``--pdb --pdb-failures -s``:**
2017-10-25 06:49:00 +08:00
```bash
2018-07-04 06:56:43 +08:00
pytest my_first_test.py --browser=chrome --pdb --pdb-failures -s
2017-10-25 06:49:00 +08:00
```
2018-08-01 02:47:10 +08:00
The code above will leave your browser window open in case there's a failure. (ipdb commands: 'c', 's', 'n' => continue, step, next).
2017-10-25 06:49:00 +08:00
#### **Pytest Reports:**
Using ``--html=report.html`` gives you a fancy report of the name specified after your test suite completes.
```bash
2018-03-05 06:55:00 +08:00
pytest my_test_suite.py --html=report.html
2017-10-25 06:49:00 +08:00
```
![](https://cdn2.hubspot.net/hubfs/100006/images/PytestReport.png "Example Pytest Report")
#### **Nosetest Reports:**
2018-03-05 06:55:00 +08:00
The ``--report`` option gives you a fancy report after your test suite completes.
2017-10-25 06:49:00 +08:00
```bash
2018-03-05 06:55:00 +08:00
nosetests my_test_suite.py --report
2017-10-25 06:49:00 +08:00
```
2018-08-22 06:27:16 +08:00
<img src="https://cdn2.hubspot.net/hubfs/100006/images/Test_Report_2.png" title="Example Nosetest Report" height="420">
2017-10-25 06:49:00 +08:00
2018-03-09 06:47:00 +08:00
(NOTE: You can add ``--show_report`` to immediately display Nosetest reports after the test suite completes. Only use ``--show_report`` when running tests locally because it pauses the test run.)
2018-08-01 02:47:10 +08:00
Here are some other useful **nosetest**-specific arguments:
```bash
--logging-level=INFO # Hide DEBUG messages, which can be overwhelming.
-x # Stop running the tests after the first failure is reached.
-v # Prints the full test name rather than a dot for each test.
--with-id # If -v is also used, will number the tests for easy counting.
```
2018-03-09 06:47:00 +08:00
#### **Using a Proxy Server:**
If you wish to use a proxy server for your browser tests (Chrome and Firefox only), you can add ``--proxy=IP_ADDRESS:PORT`` as an argument on the command line.
```bash
pytest proxy_test.py --proxy=IP_ADDRESS:PORT
```
To make things easier, you can add your frequently-used proxies to PROXY_LIST in [proxy_list.py](https://github.com/seleniumbase/SeleniumBase/blob/master/seleniumbase/config/proxy_list.py), and then use ``--proxy=KEY_FROM_PROXY_LIST`` to use the IP_ADDRESS:PORT of that key.
```bash
pytest proxy_test.py --proxy=proxy1
```