diff --git a/configure.ac b/configure.ac
index b494a969db2589039e6ab808b7619a553d3e9e60..b9416805b8071e3ad9aaceb6a0a75c27e2a25b2a 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1372,6 +1372,7 @@ AC_CONFIG_FILES([tests/testPeriodicBCPerturbed.sh], [chmod +x tests/testPeriodic
 AC_CONFIG_FILES([tests/testInteractions.sh], [chmod +x tests/testInteractions.sh])
 AC_CONFIG_FILES([tests/testParser.sh], [chmod +x tests/testParser.sh])
 AC_CONFIG_FILES([tests/testSelectOutput.sh], [chmod +x tests/testSelectOutput.sh])
+AC_CONFIG_FILES([tests/testFormat.sh], [chmod +x tests/testFormat.sh])
 
 # Save the compilation options
 AC_DEFINE_UNQUOTED([SWIFT_CONFIG_FLAGS],["$swift_config_flags"],[Flags passed to configure])
diff --git a/format.sh b/format.sh
index 9fea13bf363b1513f0e4356a67b2c9d1166771d1..7dfb038d748f44df0f2fd47f13c7989c1958e191 100755
--- a/format.sh
+++ b/format.sh
@@ -1,3 +1,80 @@
 #!/bin/bash
 
-clang-format-5.0 -style=file -i src/*.[ch] src/*/*.[ch] src/*/*/*.[ch] examples/main.c tests/*.[ch]
+# Clang format command, can be overridden using CLANG_FORMAT_CMD.
+# We currrently use version 5.0 so any overrides should provide that.
+clang=${CLANG_FORMAT_CMD:="clang-format-5.0"}
+
+# Formatting command
+cmd="$clang -style=file src/*.[ch] src/*/*.[ch] src/*/*/*.[ch] examples/main.c tests/*.[ch]"
+
+# Test if `clang-format-5.0` works
+command -v $clang > /dev/null
+if [[ $? -ne 0 ]]
+then
+    echo "ERROR: cannot find $clang"
+    exit 1
+fi
+
+# Print the help
+function show_help {
+    echo -e "This script formats SWIFT according to Google style"
+    echo -e "  -h, --help \t Show this help"
+    echo -e "  -t, --test \t Test if SWIFT is well formatted"
+}
+
+# Parse arguments (based on https://stackoverflow.com/questions/192249)
+TEST=0
+while [[ $# -gt 0 ]]
+do
+    key="$1"
+
+    case $key in
+	# print the help and exit
+	-h|--help)
+	    show_help
+	    exit
+	    ;;
+	# check if the code is well formatted
+	-t|--test)
+	    TEST=1
+	    shift
+	    ;;
+	# unknown option
+	*)
+	    echo "Argument '$1' not implemented"
+	    show_help
+	    exit
+	    ;;
+    esac
+done
+
+# Run the required commands
+if [[ $TEST -eq 1 ]]
+then
+    # Note trapping the exit status from both commands in the pipe. Also note
+    # do not use -q in grep as that closes the pipe on first match and we get
+    # a SIGPIPE error.
+    echo "Testing if SWIFT is correctly formatted"
+    $cmd -output-replacements-xml | grep "<replacement " > /dev/null
+    status=("${PIPESTATUS[@]}")
+
+    #  Trap if first command failed. Note 141 is SIGPIPE, that happens when no
+    #  output
+    if [[ ${status[0]} -ne 0 ]]
+    then
+       echo "ERROR: $clang command failed"
+       exit 1
+    fi
+
+    # Check formatting
+    if [[ ${status[1]} -eq 0 ]]
+    then
+ 	echo "ERROR: needs formatting"
+ 	exit 1
+    else
+        echo "...is correctly formatted"
+    fi
+else
+    echo "Formatting SWIFT"
+    $cmd -i
+fi
diff --git a/tests/Makefile.am b/tests/Makefile.am
index ae234581228b2ea6035af845292e9cc22d6bcaa8..d99b68f224f542dcdc60ae59fc6e2042ae20d9b7 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -28,7 +28,7 @@ TESTS = testGreetings testMaths testReading.sh testSingle testKernel testSymmetr
         testVoronoi1D testVoronoi2D testVoronoi3D testGravityDerivatives \
 	testPeriodicBC.sh testPeriodicBCPerturbed.sh testPotentialSelf \
 	testPotentialPair testEOS testUtilities testSelectOutput.sh \
-	testCbrt testCosmology testOutputList
+	testCbrt testCosmology testOutputList testFormat.sh
 
 # List of test programs to compile
 check_PROGRAMS = testGreetings testReading testSingle testTimeIntegration \
diff --git a/tests/testFormat.sh.in b/tests/testFormat.sh.in
new file mode 100644
index 0000000000000000000000000000000000000000..1d0fdeb1334ea7e9ac7b6605c23d7567a2c8c62b
--- /dev/null
+++ b/tests/testFormat.sh.in
@@ -0,0 +1,6 @@
+#!/bin/bash
+
+set -e
+
+cd @srcdir@/..
+./format.sh --test