Paul C
Paul C

Reputation: 51

PyCharm Nosetests - unrecognised tests

I have various forms of this issue repeatedly with PyCharm and nosetest integration.

Right click does not recognise certain tests or that the class/module contains nosetests at all.

I have searched through many, many SO answers and still haven't got to the bottom of this.

Here is the current reproduction of the issue. The content of the tests is not really relevant, but it is testing a singleton wrapper around a DB interface.

class MyDBTest(unittest.TestCase):

  def setup(self):
    self.db = db_utils.get_connection(constants)

  def teardown(self):
    db_utils.truncate_all_my_tables(self.db)
    self.db.commit()
    self.db.close()

  def test_same_instance(self):
    other_instance = db_utils.get_connection(constants)
    assert other_instance is self.db

  def test_basic_query(self):
    cur = self.db.cursor()
    sql = "SELECT %s"
    cur.execute(sql, "Test")
    assert cur.rowcount == 1, "Expected one row, got <{0!s}>".format(cur.rowcount)

  def test_reopen_connection(self):
    self.db.close()
    self.db = db_utils.get_connection(constants)
    cur = self.db.cursor()
    sql = "SELECT %s"
    cur.execute(sql, "Test")
    assert cur.rowcount == 1, "Expected one row, got <{0!s}>".format(cur.rowcount)

If I right click on the test "test_basic_query" I get the context menu option: "Run Nosetest test_basic_query" and clicking it does indeed run the test, including the setup and teardown.

However right clicking on the other tests only presents the "run test_db_wrapper" option. Right clicking on the file in the Project pane only provides this option as well.

Running this file from the command line with the nosetests3 executable does something even weirder. It runs all three tests, but does not run the setup and teardown.

Note I have tried re-ordering, renaming the tests. I have tried inheriting from object, nothing or unittest.TestCase. I have other test classes that look exactly like this and they work.

This is just one example, this happens to us all the time. It just seems random.

PyCharm version is: PyCharm Community Edition 2016.1.4 Build #PC-145.1504, built on May 25, 2016 JRE: 1.8.0_76-release-b198 amd64 JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o

Upvotes: 1

Views: 1246

Answers (2)

phil_20686
phil_20686

Reputation: 4080

If it previously didnt recognise it and it created a run configuration then you may have to delete that run configuration for it to detect the tests as tests and not as a script. Look in the drop down menu:

enter image description here

And you should find a list of existing run conficurations. If Pycharm already has one that has the name of your file it will treat that as the default run configuration. (This is good as it means you can create and persist custom run configurations) but it sometimes means that if you already ran a test file as a script the context menu will not change back. Go into edit in that drop down and you can delete any existing run configuration.

Upvotes: 0

Paul C
Paul C

Reputation: 51

So far here are the things I have found to prevent the above scenario:

  1. The test class name should begin with "Test", "TestFoo" for example
  2. The test filename should begin with test, test_my_class.py for example
  3. Test methods should start with the word test, "test_no_results(self)" for example. Similarly non-test methods should not contain test, or setup or teardown (for simplicity) - create_test_data(self): for example
  4. PyCharm can be a bit weird and can take a while to recognise changes you make to test classes, so if you verify the tests (including setup and teardown) run by executing "nosetests" from the command line but don't work in PyCharm try restarting PyCharm and clearing the cache.
  5. You DO NOT need to extend unitest.TestCase to use Nosetests. In fact doing so can make your life more complicated with regards to itelisense and autocomplete method names when editing.

Upvotes: 2

Related Questions