To receive notifications about scheduled maintenance, please subscribe to the mailing-list gitlab-operations@sympa.ethz.ch. You can subscribe to the mailing-list at https://sympa.ethz.ch

Commit d6327780 authored by vermeul's avatar vermeul
Browse files

added fixtures

parent 7a73db68
......@@ -15,3 +15,45 @@ $ pytest tests/test_module.py::test_current_work
$ pytest tests/ -k "MyClass and not weird_test"
$ pytest tests/ -k "test_current_work"
```
## Fixtures
* create a file named `conftest.py` within the `tests` folder
* add one or many fixtures as shown below
* set the `scope` to either `function`, `class`, `module`, `package` or `session` (see https://pytest.org/en/latest/how-to/fixtures.html#fixture-scopes)
* typically, database connections or other slow operations are `scope=session` to save time
```python
import pytest
import os
@pytest.fixture(scope="session")
def mysql_conn():
db_host = os.getenv("DB_HOST")
db_name = os.getenv("DB_NAME")
db_user = os.getenv("DB_USER")
db_password = os.getenv("DB_PASSWORD")
engine = create_engine(
f"mysql+pymysql://{db_user}:{db_password}@{db_host}:3306/{db_name}"
)
connection = engine.connect()
yield connection
connection.close()
```
And then use fixture in your testing module(s), using the `mysql_conn` fixture as the first parameter:
```python
import pytest
def test_select_something(mysql_conn):
mycursor = mysql_conn.cursor()
mycursor.execute("SELECT * FROM customers")
myresult = mycursor.fetchall()
...
```
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment