OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How can I ensure some code is run even if a test suite is aborted?

  • Thread starter Thread starter spierepf
  • Start date Start date
S

spierepf

Guest
I have a Behave test suite that starts by spinning up my application in a Docker container. I've added some code using atexit to ensure that the container is stopped and removed when the suite exits 'normally':

Code:
def handle_exit():
    context.container.stop()
    context.docker_client.close()

atexit.register(handle_exit)

This works fine as long as the test suite exits normally, even with failing tests. But sometimes the tests are aborted:

Code:
HOOK-ERROR in before_all: FileNotFoundError: [Errno 2] No such file or directory: '/home/peter/Nextcloud/PycharmProjects/boardgamelibrary/boardgamelibrary'
HOOK-ERROR in after_all: AttributeError: 'Context' object has no attribute 'driver'

ABORTED: By user.

in which case the container is not cleaned up, and subsequent attempts to run the suite will fail.

Is there a stronger version of atexit that will run my handle_exit routine in all exit/abort scenarios?
<p>I have a Behave test suite that starts by spinning up my application in a Docker container. I've added some code using <a href="https://docs.python.org/3/library/atexit.html" rel="nofollow noreferrer"><code>atexit</code></a> to ensure that the container is stopped and removed when the suite exits 'normally':</p>
<pre><code>def handle_exit():
context.container.stop()
context.docker_client.close()

atexit.register(handle_exit)
</code></pre>
<p>This works fine as long as the test suite exits normally, even with failing tests. But sometimes the tests are aborted:</p>
<pre class="lang-none prettyprint-override"><code>HOOK-ERROR in before_all: FileNotFoundError: [Errno 2] No such file or directory: '/home/peter/Nextcloud/PycharmProjects/boardgamelibrary/boardgamelibrary'
HOOK-ERROR in after_all: AttributeError: 'Context' object has no attribute 'driver'

ABORTED: By user.
</code></pre>
<p>in which case the container is not cleaned up, and subsequent attempts to run the suite will fail.</p>
<p>Is there a <em>stronger</em> version of <code>atexit</code> that will run my <code>handle_exit</code> routine in all exit/abort scenarios?</p>
 

Latest posts

Online statistics

Members online
0
Guests online
4
Total visitors
4
Top