Get name of current test in setup using nose

2024/11/13 8:00:23

I am currently writing some functional tests using nose. The library I am testing manipulates a directory structure.

To get reproducible results, I store a template of a test directory structure and create a copy of that before executing a test (I do that inside the tests setup function). This makes sure that I always have a well defined state at the beginning of the test.

Now I have two further requirements:

  1. If a test fails, I would like the directory structure it operated on to not be overwritten or deleted, so that I can analyze the problem.
  2. I would like to be able to run multiple tests in parallel.

Both these requirements could be solved by creating a new copy with a different name for each test that is executed. For this reason, I would like to get access to the name of the test that is currently executed in the setup function, so that I can name the copy appropriately. Is there any way to achieve this?

An illustrative code example:

def setup_func(test_name):print "Setup of " + test_namedef teardown_func(test_name):print "Teardown of " + test_name@with_setup(setup_func, teardown_func)
def test_one():pass@with_setup(setup_func, teardown_func)
def test_two():pass

Expected output:

Setup of test_one
Teardown of test_one
Setup of test_two
Teardown of test_two

Injecting the name as a parameter would be the nicest solution, but I am open to other suggestions as well.

Answer

Sounds like self._testMethodName or self.id() should work for you. These are property and method on unittest.TestCase class. E.g.:

from django.test import TestCaseclass MyTestCase(TestCase):def setUp(self):print self._testMethodNameprint self.id()def test_one(self):self.assertIsNone(1)def test_two(self):self.assertIsNone(2)

prints:

...
AssertionError: 1 is not None
-------------------- >> begin captured stdout << ---------------------
test_one
path.MyTestCase.test_one--------------------- >> end captured stdout << ----------------------
...
AssertionError: 2 is not None
-------------------- >> begin captured stdout << ---------------------
test_two
path.MyTestCase.test_two--------------------- >> end captured stdout << ----------------------

Also see:

  • A way to output pyunit test name in setup()
  • How to get currently running testcase name from testsuite in unittest

Hope that helps.

https://en.xdnf.cn/q/72203.html

Related Q&A

python: find html tags and replace their attributes [duplicate]

This question already has answers here:Replace SRC of all IMG elements using Parser(2 answers)Closed 10 years ago.I need to do the following:take html document find every occurrence of img tag take the…

Django/Apache/mod_wsgi not using virtualenvs Python binary

I have a virtualenv at /opt/webapps/ff/ with its own Python installation. I have WSGIPythonHome set to /opt/webapps/ff in my Apache config file (and this is definitely getting used in some capacity, b…

How to open the users preferred mail application on Linux?

I wrote a simple native GUI script with python-gtk. Now I want to give the user a button to send an email with an attachment.The script runs on Linux desktops. Is there a way to open the users preferr…

finding a set of ranges that a number fall in

I have a 200k lines list of number ranges like start_position,stop position. The list includes all kinds of overlaps in addition to nonoverlapping ones.the list looks like this[3,5] [10,30] [15,25] [5…

Python Tornado Websocket Connections still open after being closed

I have a Tornado Websocket Server and I want to time out after 30 minutes of inactivity. I use self.close() to close the connection after 30 minutes of inactivity. But it seems that some connections st…

Vertical Print String - Python3.2

Im writing a script that will take as user inputed string, and print it vertically, like so:input = "John walked to the store"output = J w t t so a o h th l e on k re edIve written …

How to remove small particle background noise from an image?

Im trying to remove gradient background noise from the images I have. Ive tried many ways with cv2 without success.Converting the image to grayscale at first to make it lose some gradients that may hel…

Running commands from within python that need root access

I have been playing around with subprocess lately. As I do more and more; I find myself needing root access. I was wondering if there is an easy way to enter the root password for a command that needs …

How not to plot missing periods

Im trying to plot a time series data, where for certain periods there is no data. Data is loaded into dataframe and Im plotting it using df.plot(). The problem is that the missing periods get connected…

Disabling std. and file I/O in Python sandbox implementation

Im trying to set up a Python sandbox and want to forbid access to standard and file I/O. I am running the sandbox inside of a running Python server.Ive already looked at modules like RestrictedPython a…