https://pantsbuild.org/ logo
#general
Title
# general
a

ambitious-petabyte-59095

02/17/2022, 4:39 PM
Hi there, I am having trouble with some tests and was wondering if anyone can guide me. My tests need files created at the start of the session and drop this test database on teardown. However, it looks like the files are not created (gridfs no file found). The same tests will succeed from the container (
docker exec -t projectA-container pytest tests projectA)
, which is one session. This is my current project structure:
Copy code
projectA
  - tests
     - units
         - __init__.py
         - BUILD # tests
         - test_task1.py
     - utils
         - __init__.py
         - BUILD  
         - init_data.py
     __init__.py
     BUILD # test utils
     conftest.py
this is the relevant part of my
conftest.py
Copy code
@pytest.fixture(scope="session", autouse=True)
def mongo():
    client = MongoClient(settings.MONGO_URI)
    db = client["test"]
    yield db
    client.drop_database("test")
I know that the tests are executed in parallel given enough cores so I was wondering if this could possibly affect the "session" scope? Any help to better understand this would be appreciated. Thanks so much!
👍 1
e

enough-analyst-54434

02/17/2022, 4:52 PM
Yeah, Pants breaks pytest scopes expectations because it runs each test file in a separate process. I'm not sure we have a solution for this. There is a solution for dealing with test concurrency when an underlying resource like a database is not itself concurrent via https://www.pantsbuild.org/docs/reference-pytest#section-execution-slot-var. An example of using that for a sqlite database to power Django tests is here: + Pants configuration: https://github.com/pantsbuild/example-django/blob/2b0df09201a53ede07df62a8169ac78ac3203019/pants.toml#L52 + conftest: https://github.com/pantsbuild/example-django/blob/2b0df09201a53ede07df62a8169ac78ac3203019/helloworld/translate/conftest.py#L6 + Use of execution_slot_var: https://github.com/pantsbuild/example-django/blob/2b0df09201a53ede07df62a8169ac78ac3203019/helloworld/util/settings_for_tests.py#L21-L24
👍 1
@ambitious-petabyte-59095 what I expect we don't have a solution for is "session" in the way you hope since "session" means per-test-file under Pants execution model. That should just mean though that your tests run slower individually, I wouldn't think it leads to file not found issues. Can you give more details on the error? What creates the gridfs files? Are those files owned by a Pants BUILD target that the failing test depends upon?
🤕 1
a

ambitious-petabyte-59095

02/18/2022, 2:13 PM
You're right. After some investigation, it seems the issue originated from another part in my files initialization code, which had to be tweaked a bit based on the new monorepo setup. Now, although slower, they are passing like intended. Thanks so much for your insight and help.