3
votes

I'm new to unit testing and "im trying to run a pytest on Eclipse. I have searched for hours now and I cant seem to find out what the problem is. Im playing around with the simple examples from the https://pytest.org website. My problem is that pytest just does not run on Eclipse. I can use the command prompt to do the tests, but I would much rather have the results on the console window.

Things I have tried but didnt work;

  • setting PyUnit test runner to Py.test runner (instead of the default Pydev test runner)

In this case I get the following error message

usage: runfiles.py [options] [file_or_dir] [file_or_dir] [...] runfiles.py: error: unrecognized arguments: --verbosity inifile: None rootdir: C:\peepee\pytest\testing

  • I have set the verbosity to 9 (read somewhere that its the maximum). Didnt make any difference.

Simple code I'm trying to test from the http://pytest.org website

def func(x):
    return x + 1

def test_answer():
    assert func(3) == 5

Works through the cmd but not on Eclipse.

Please help, as I'm losing time on trying to figure this out. Thanks in advance

4

4 Answers

3
votes

It didn't work for you because you left "--verbosity 0" in the parameters text field. I don't know why it's not automatically erased by Eclipse, but when you change the runner you MUST also change the parameters to reflect your preferred test runner (pytest in this case).

Globally for all new configurations:

  1. Window -> Preferences -> PyDev -> PyUnit
  2. Change the test runner to "py.test runner" and clear the parameters (or add the ones you prefer. Make sure they are valid flags for pytest.)

Or, if you prefer, manually for each new run configuration

  1. Create a Python unittest run configuration
  2. Select the class you want to run and the project (if they are not already there)
  3. In the Arguments tab override the pyUnit preferences with a py.test runner (clean up the parameters and add whatever you want to add to pytest flags)
1
votes

1) open run configurations from run menu

2) right click on python unittest and select new to configure a new configuration

3) select the project and the in the main module, select the module which has your test cases.

4) under arguments tab, check 'override pyunit preferences for this launch'.

5) select Py.test runner from the drop down.

6) type --tb=short and --capture=no (values can be changed depending on user preference)

7) click apply and then click run.

0
votes

NOTE: If you are using django-configurations for your settings then you MUST set the DJANGO_CONFIGURATION environment variable in the environment variables section of the debugger configuration to whatever you use for your testing runs from the command line.

-3
votes

Found an alternative. Just use the PyCharm IDE which makes pytest very easy to run. Make sure to do the following configuration before running any test.

  • Click the "Run" tab. Select "Edit configurations"
  • Add configuration with the "+" symbol
  • Select "Python test" under that "py.test"
  • Make sure to fill out the "Target" path and the "working directory"

Happy days. Now you have pytest running with the results displayed on the console window