0
0
PyTesttesting~15 mins

Handling shared resources in PyTest - Build an Automation Script

Choose your learning style9 modes available
Test shared resource access with setup and teardown
Preconditions (2)
Step 1: Before tests, create or clear the file 'shared_resource.txt'
Step 2: Test 1: Write 'Test1' to the file
Step 3: Verify the file contains 'Test1'
Step 4: Test 2: Append 'Test2' to the file
Step 5: Verify the file contains both 'Test1' and 'Test2' in order
Step 6: After tests, delete the file 'shared_resource.txt'
✅ Expected Result: Both tests pass, confirming that the shared resource is properly handled with setup and teardown to avoid test interference.
Automation Requirements - pytest
Assertions Needed:
Verify file content after each test matches expected text
Best Practices:
Use pytest fixtures with scope='module' or 'function' to setup and teardown shared resources
Avoid test interference by cleaning or resetting shared resources before each test
Use explicit file open modes to control write/append behavior
Automated Solution
PyTest
import os
import pytest

@pytest.fixture(scope='module')
def shared_file():
    filename = 'shared_resource.txt'
    # Setup: create or clear the file before tests
    with open(filename, 'w') as f:
        f.write('')
    yield filename
    # Teardown: remove the file after tests
    if os.path.exists(filename):
        os.remove(filename)


def test_write_test1(shared_file):
    with open(shared_file, 'w') as f:
        f.write('Test1')
    with open(shared_file, 'r') as f:
        content = f.read()
    assert content == 'Test1', f"Expected 'Test1' but got '{content}'"


def test_append_test2(shared_file):
    with open(shared_file, 'a') as f:
        f.write('Test2')
    with open(shared_file, 'r') as f:
        content = f.read()
    assert content == 'Test1Test2', f"Expected 'Test1Test2' but got '{content}'"

This code uses a pytest fixture named shared_file with scope='module' to setup and teardown a shared file resource.

Before any tests run, the fixture creates or clears the file shared_resource.txt. After all tests in the module finish, it deletes the file to clean up.

The first test writes 'Test1' to the file and verifies the content exactly matches 'Test1'.

The second test appends 'Test2' to the same file and verifies the content is 'Test1Test2', confirming the append worked and the shared resource was handled correctly.

This approach avoids interference by ensuring the file starts empty before tests and is removed after, following best practices for shared resource handling.

Common Mistakes - 4 Pitfalls
Not cleaning the shared resource before tests
Using global variables to share resource state
Not deleting or cleaning up the shared resource after tests
{'mistake': "Opening files in wrong mode (e.g., always 'w' instead of 'a' for append)", 'why_bad': 'Data can be overwritten unintentionally, causing test failures.', 'correct_approach': "Use correct file modes ('w' for write, 'a' for append) depending on test needs."}
Bonus Challenge

Now add data-driven testing with 3 different strings to write and append to the shared file

Show Hint