question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Test Results directory in setup fails intermittently

See original GitHub issue

This issue is not consistent, but sometimes the Test Results directories are not created or, at least, not created in time causing the setup to fail. This looks like an issue with race conditions, but right now we have

  • pytest-xdist
  • pytest-parallel

as the two plugins that allow you to run tests in parallel. This feature is considered “legacy” since it was one of the first features written. These folders were used to store log files and screenshots for each test, but logging now happens via the logging module so now these folders are only used for screenshots if the test fails.

So, we can either fix this issue, or we can create a new Feature Request suggesting a better, cleaner way to report logs and screenshots on failure.

project_root = '/home/runner/work/pyleniumio/pyleniumio'
request = <SubRequest 'test_run' for <Function test_element_parent_and_siblings>>

    @pytest.fixture(scope='session', autouse=True)
    def test_run(project_root, request) -> str:
        """ Creates the `/test_results` directory to store the results of the Test Run.
    
        Returns:
            The `/test_results` directory as a filepath (str).
        """
        session = request.node
        test_results_dir = f'{project_root}/test_results'
    
        if os.path.exists(test_results_dir):
            # delete /test_results from previous Test Run
            shutil.rmtree(test_results_dir, ignore_errors=True)
        if not os.path.exists(test_results_dir):
            # create /test_results for this Test Run
            make_dir(test_results_dir)
    
        for test in session.items:
            # make the test_result directory for each test
>           make_dir(f'{test_results_dir}/{test.name}')

conftest.py:111: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

filepath = '/home/runner/work/pyleniumio/pyleniumio/test_results/test_element_with_no_siblings'

    def make_dir(filepath) -> bool:
        """ Make a directory.
    
        Returns:
            True if successful, False if not.
        """
        try:
>           os.mkdir(filepath)
E           FileNotFoundError: [Errno 2] No such file or directory: '/home/runner/work/pyleniumio/pyleniumio/test_results/test_element_with_no_siblings'

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
ElSnoMancommented, May 21, 2021

@chingc I believe I’ve fixed this issue in v1.12.5, but I’d love for you to try it out! I always appreciate your feedback 😄

0reactions
chingccommented, Jun 4, 2021

@ElSnoMan Nevermind, I just hit this today. 😦

I’m running using pytest -n 5 so I think what’s happening is the 5 workers clobbering each other.

I think the race condition is happening between the time Path is checking to see if the file exists and when it attempts to create it. I think adding a try/except might fix this problem for good. For example:

for test in session.items:
    try:
        # make the test_result directory for each test
        Path(f'{test_results_dir}/{test.name}').mkdir(parents=True, exist_ok=True)
    except FileExistsError:
        # avoid race condition between checking file existence and creating the file when using pytest with multiple workers
        pass

Below is the error I got.

self = PosixPath('/home/ubuntu/bentobox/test_results/test_patch_is_active')
mode = 511, parents = True, exist_ok = True

    def mkdir(self, mode=0o777, parents=False, exist_ok=False):
        """
        Create a new directory at this given path.
        """
        if self._closed:
            self._raise_closed()
        try:
>           self._accessor.mkdir(self, mode)
E           FileExistsError: [Errno 17] File exists: '/home/ubuntu/bentobox/test_results/test_patch_is_active'

/usr/lib/python3.8/pathlib.py:1284: FileExistsError
_ ERROR at setup of GoogleInventoryAPIServiceTestCase.test_incorrect_update_response_will_log_warning _
[gw1] linux -- Python 3.8.5 /home/ubuntu/venv/bin/python3

project_root = '/home/ubuntu/bentobox'
request = <SubRequest 'test_run' for <TestCaseFunction test_update_when_valid>>

    @pytest.fixture(scope='session', autouse=True)
    def test_run(project_root, request) -> str:
        """Creates the `/test_results` directory to store the results of the Test Run.
    
        Returns:
            The `/test_results` directory as a filepath (str).
        """
        session = request.node
        test_results_dir = f'{project_root}/test_results'
    
        if os.path.exists(test_results_dir):
            # delete /test_results from previous Test Run
            shutil.rmtree(test_results_dir, ignore_errors=True)
    
        Path(test_results_dir).mkdir(parents=True, exist_ok=True)
    
        for test in session.items:
            # make the test_result directory for each test
>           Path(f'{test_results_dir}/{test.name}').mkdir(parents=True, exist_ok=True)

conftest.py:99: 
Read more comments on GitHub >

github_iconTop Results From Across the Web

Why are all my Visual Studio test results "Not executed"
Open the Visual Studio command prompt · Change to the directory where the binary output of your test project is. · Type mstest...
Read more >
How to Find Failures Faster in Jenkins Test Results - TotalView
Diagnosing and fixing failures in Jenkins test results is time ... Intermittent failures: The test failure is intermittent and hard to ...
Read more >
Intermittent failure of Azure AD Connect directory ...
I have an installation of Azure AD Connect on an on-premises server that has been running error-free for several years. It has suddenly...
Read more >
Active Directory ADCs intermittently fail with 'LDAP response ...
Issue. Active Directory ADCs intermittently fail with the following error when reading data (users and/or groups):. LDAP response read timed out.
Read more >
Why do Apex Unit Test CLASSES sometimes FAIL when all ...
To resolve: click the "View Test History" link on this page, then press the "Clear Test Results" button as many times as it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found