I am using Playwright Python for end-to-end testing of my docker-compose project.
The compose file looks like this:
services:
project-database:
...
hostname: database-host
networks:
- project-network
project-frontend:
...
hostname: frontend-host
networks:
- project-network
project-backend:
...
hostname: backend-host
networks:
- project-network
project-tests:
...
container_name: project-tests
networks:
- project-network
To execute my tests I do:
> docker compose build
> docker compose up -d
> docker compose run --rm project-tests pytest /playwright/tests
With the mysql-connector I can connect to the DB with connection = mysql.connector.connect(user= 'admin', host= 'database-host', (..)
and then assert stuff from my webpage with the content of my database.
Example for one of the tests in /playwright/tests:
from playwright.sync_api import Page, expect
def simple_test(page: Page) -> None:
page.goto(http://frontend-host/articles)
page.locator("#new-article-name").fill("Sample")
page.locator("#create-new-article-button").click()
connection.cursor.execute("SELECT * FROM article")
article = cursor.fetchone()
expect( page.locator("#article-list") ).to_contain_text( article[1])
So far so good, but for testing purposes, I want to control the database, for example resetting after tests as not to cross-contaminate the test results or loading different scenarios before testing.
My SQL files are located in the database Docker container under /schemas/.
I tried using the mysql-connector like connection.cursor.execute("SOURCE /schemas/empty.sql")
in an after_script which results in a syntax error (got the idea from /a/21796949/20208551 )
I also tried subprocess.run( cmd.split() )
with cmd = "mysql -h database-host -u user -ppassword database < /schemas/empty.sql"
and while it tells me not to use my password on the commandline, it doesn’t do anything else.
connection.start_transaction()
with connection.rollback()
also doesn’t do anything, (and shouldn’t in this case if I understand it correctly but I am trying anything at this point)
Is there a simple way to do this?