diff --git a/tests/README.md b/tests/README.md new file mode 100644 index 00000000..6a2a792b --- /dev/null +++ b/tests/README.md @@ -0,0 +1,68 @@ +# novelWriter Tests + +The test suite uses PyTest for testing. + +## Dependencies + +* `python3-pytest` for the basic framework (required) +* `python3-pytestqt` for Qt support (required) +* `python3-pytest-cov` for code coverage reports (optional) +* `python3-pytest-xvfb` for headless tests (optional) + +## HowTo + +### Basic Usage + +To run all tests, type: +```bash +pytest-3 -v +``` + +The `-v` switch enables verbose mode, with one test per line. +For a more compact view, omit this switch. + +### Headless + +To run tests in headless mode, either use the Qt `offscreen` mode: +```bash +export QT_QPA_PLATFORM=offscreen +``` + +or run with `xvfb`: +```bash +xvfb-run pytest-3 -v +``` + +### Test Coverage + +To add test coverage, run the following: +```bash +pytest-3 -v --cov=nw --cov-report=html +``` + +The `--cov-report` switch generates an html report, omit it to print a coverage summary to the terminal. +The html coverage report will be available in the `htmlcov` folder. + +### Test Markers (Categories) + +To run with specific test markers, add the `-m` switch: +```bash +pytest-3 -v -m core +``` + +Available markers are: + +* '`core`' for test covering the classes in the `nw/core` folder + +## Tests + +To filter specific groups of tests, use the `-k` switch. +The commands for the respective test categories are listed below. + +| Type | Test Target | Source File(s) | Marker | Filter | +| :--- | :------------- | :--------------- | :-------- | :----------------- | +| Unit | Core functions | nw/core/tools.py | `-m core` | `-k testCoreTools` | +| Unit | NWIndex class | nw/core/index.py | `-m core` | `-k testCoreIndex` | +| Unit | NWItem class | nw/core/item.py | `-m core` | `-k testCoreItem` | +| Unit | NWTree class | nw/core/tree.py | `-m core` | `-k testCoreTree` | + diff --git a/tests/conftest.py b/tests/conftest.py index 2d2ec6e8..0e59a3dc 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -26,12 +26,12 @@ def tmpDir(): be checked. The folder is instead cleared before a new test session. """ testDir = os.path.dirname(__file__) - tempDir = os.path.join(testDir, "temp") - if os.path.isdir(tempDir): - shutil.rmtree(tempDir) - if not os.path.isdir(tempDir): - os.mkdir(tempDir) - return tempDir + theDir = os.path.join(testDir, "temp") + if os.path.isdir(theDir): + shutil.rmtree(theDir) + if not os.path.isdir(theDir): + os.mkdir(theDir) + return theDir @pytest.fixture(scope="session") def refDir(): @@ -39,8 +39,17 @@ def refDir(): the results of tests. """ testDir = os.path.dirname(__file__) - refDir = os.path.join(testDir, "reference") - return refDir + theDir = os.path.join(testDir, "reference") + return theDir + +@pytest.fixture(scope="session") +def outDir(tmpDir): + """An output folder for test results + """ + theDir = os.path.join(tmpDir, "results") + if not os.path.isdir(theDir): + os.mkdir(theDir) + return theDir ## # novelWriter Objects diff --git a/tests/test_core_index.py b/tests/test_core_index.py index 74bdd899..1d577b70 100644 --- a/tests/test_core_index.py +++ b/tests/test_core_index.py @@ -15,12 +15,12 @@ from nw.core.index import NWIndex from nw.constants import nwItemClass, nwItemLayout @pytest.mark.core -def testCoreIndex_LoadSave(monkeypatch, nwLipsum, dummyGUI, nwTempProj, refDir): +def testCoreIndex_LoadSave(monkeypatch, nwLipsum, dummyGUI, outDir, refDir): """Test core functionality of scaning, saving, loading and checking the index cache file. """ projFile = os.path.join(nwLipsum, "meta", "tagsIndex.json") - testFile = os.path.join(nwTempProj, "coreIndex_LoadSave_tagsIndex.json") + testFile = os.path.join(outDir, "coreIndex_LoadSave_tagsIndex.json") compFile = os.path.join(refDir, "coreIndex_LoadSave_tagsIndex.json") theProject = NWProject(dummyGUI)