diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index cb33af4d972dd398952b1879cca764c502d8f42c..51d0aa9c97644c3e3ade1d35f6346c0c44bf7c77 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -39,6 +39,7 @@ set(VALIDATION_SOURCES
     validate.cpp
 )
 
+add_definitions("-DDATADIR=\"${CMAKE_SOURCE_DIR}/data\"")
 add_executable(test.exe ${TEST_SOURCES} ${HEADERS})
 add_executable(validate.exe ${VALIDATION_SOURCES} ${HEADERS})
 
diff --git a/tests/test_swcio.cpp b/tests/test_swcio.cpp
index c3fc24d3926c86abd1ed3be760c1c6f5b7316ced..62c52b099e6142f9bc409ca461568dbce7ffa4d7 100644
--- a/tests/test_swcio.cpp
+++ b/tests/test_swcio.cpp
@@ -11,6 +11,11 @@
 #include "../src/cell.hpp"
 #include "../src/swcio.hpp"
 
+// Path to data directory can be overriden at compile time.
+#if !defined(DATADIR)
+#define DATADIR "../data"
+#endif
+
 // SWC tests
 void expect_record_equals(const nest::mc::io::swc_record &expected,
                           const nest::mc::io::swc_record &actual)
@@ -227,7 +232,8 @@ TEST(swc_parser, from_allen_db)
 {
     using namespace nest::mc;
 
-    auto fname = "../data/example.swc";
+    std::string datadir{DATADIR};
+    auto fname = datadir+"/example.swc";
     std::ifstream fid(fname);
     if(!fid.is_open()) {
         std::cerr << "unable to open file " << fname << "... skipping test\n";
@@ -493,7 +499,8 @@ TEST(swc_io, cell_construction)
 // the one generated with the C++ interface
 TEST(swc_parser, from_file_ball_and_stick)
 {
-    auto fname = "../data/ball_and_stick.swc";
+    std::string datadir{DATADIR};
+    auto fname = datadir+"/ball_and_stick.swc";
     std::ifstream fid(fname);
     if(!fid.is_open()) {
         std::cerr << "unable to open file " << fname << "... skipping test\n";
diff --git a/tests/test_tree.cpp b/tests/test_tree.cpp
index c27c1674adbb4c5314be9ad0f4d65b80b06a2677..1e06b3a90fdf92f96e43e2c93036d37788c3efa6 100644
--- a/tests/test_tree.cpp
+++ b/tests/test_tree.cpp
@@ -8,6 +8,11 @@
 
 #include "../src/cell_tree.hpp"
 
+// Path to data directory can be overriden at compile time.
+#if !defined(DATADIR)
+#define DATADIR "../data"
+#endif
+
 using json = nlohmann::json;
 using range = memory::Range;
 
@@ -287,7 +292,10 @@ TEST(cell_tree, balance) {
 TEST(cell_tree, json_load)
 {
     json  cell_data;
-    std::ifstream("../data/cells_small.json") >> cell_data;
+    std::string path{DATADIR};
+
+    path += "/cells_small.json";
+    std::ifstream(path) >> cell_data;
 
     for(auto c : range(0,cell_data.size())) {
         std::vector<int> parent_index = cell_data[c]["parent_index"];