How to unit test Neo4j in python ?

I'm going to say the easiest way would be to mock out Neo4j entirely.

What I like to do is to simplify your entity classes to as little pure-python functionality as possible.

Even if you're using some fancy ORM to access your data - for example in Django - I still prefer to create a pure-python class to represent my model and create a minimalistic dao method, which will be mocked-out during unit tests. That way I don't have to ever touch the database in my unit tests, which I think is how unit tests should behave.

That being said, if you really want to have neo4j functionality in your tests, have a look at the IntegrationTestCase of the official neo4j-python-driver package. It looks like it is providing a base class to inherit your integration tests (because if you're pulling the DB into your tests, you're essentially doing Integration Tests) from, as it takes care of starting up and shutting down the server between test runs.


More answers have been provided via Neo4j community: https://community.neo4j.com/t/is-there-a-library-to-unit-test-graphdb-cypher-queries-using-python/8227/5

(Easy) Check your cypher syntax using libcypher-parser

You can also put your queries into .cypher files and use a linter like libcypher-parser (https://github.com/cleishm/libcypher-parser) to check that your queries are valid.

(Advanced) Spin up a docker instance

Use a simple docker container to spin up another Neo4j instance, then you can test to your heart's content ... There you can (in this sandboxed environment):

  • just run your cypher queries to see if they work
  • populate/seed the sandboxed Neo4j database instance and run more substantial tests