Unit test fails in template app

Hi all!

Beginner here. I’m learning how to develop Nextcloud apps. After generating a template app, I try to run the unit test by typing phpunit tests/. I get this result:

PHP Fatal error:  Uncaught Error: Class 'OCA\NoNoNotes\Tests\Unit\Controller\NoteControllerTest' not found in /home/jon/code/nextcloud-dev/apps/nononotes/tests/Unit/Controller/NoteApiControllerTest.php:10
Stack trace:
#0 /usr/share/php/PHPUnit/Util/FileLoader.php(65): include_once()
#1 /usr/share/php/PHPUnit/Util/FileLoader.php(49): PHPUnit\Util\FileLoader::load()
#2 /usr/share/php/PHPUnit/Framework/TestSuite.php(402): PHPUnit\Util\FileLoader::checkAndLoad()
#3 /usr/share/php/PHPUnit/Framework/TestSuite.php(530): PHPUnit\Framework\TestSuite->addTestFile()
#4 /usr/share/php/PHPUnit/Runner/BaseTestRunner.php(98): PHPUnit\Framework\TestSuite->addTestFiles()
#5 /usr/share/php/PHPUnit/TextUI/Command.php(119): PHPUnit\Runner\BaseTestRunner->getTest()
#6 /usr/share/php/PHPUnit/TextUI/Command.php(95): PHPUnit\TextUI\Command->run()
#7 /usr/bin/phpunit(42): PHPUnit\TextUI\Command::main()
#8 {main}

Next PHPUnit\TextUI\RuntimeException: Class 'OCA\NoNoNotes\Tests\Unit\Controller\NoteControllerTest' not found in /usr/share/php/PHPUnit/Tex in /usr/share/php/PHPUnit/TextUI/Command.php on line 97

There is a class NoteApiControllerTest extends NoteControllerTest in the file /home/jon/code/nextcloud-dev/apps/nononotes/tests/Unit/Controller/NoteApiControllerTest.php

Is there an error in the template app or did I do something wrong?

Hello,

there is a configuration in tests/phpunit.xml (also for the integration tests). This will load the NC kernel in order to find all the class files. You need to use this configuration file instead of plain PHPUnit to test your classes.

You might want to read this topic before continuing to experiment. It might be the best time to set up a dev environment before digging into code and tests.

A quick test on my machine showed that I needed an installed NC server locally with the tests included. So, I needed to fetch the server core with git first

git clone --depth 1 https://github.com/nextcloud/server.git

Then, I needed to go inside and load the submodules (thus you needed to go the git way not just a ZIP)

cd server
git submodule update --init --depth 1

Install that server locally: Prepare a DB, etc. You will need that anyways for debugging. You can use the debugging environment.

Once everything is in place, you can run in the app’s folder

phpunit -c tests/phpunit.xml

I hope this helps for now.
Christian

1 Like

Thanks for your help Christian!

How do you mean? Should I install this inside the container?

Ahh, sorry for the late reply.

There are two aspects here.

  1. You will most probably need a running server to test your app. See the link provided, the tutorials and other resources on the internet.
  2. All classes and interfaces need to be known in order to use PHPUnit. These are part of the server core and that one needs to be available.

For 2. you need an installed server instance. You need the development variant (from git) not just the tarball. I tried without prior installation and was presented with an exception.

If you are using a container or not for the complete process is up to you. I would personally prefer to do so but, yeah, this can be done either way.