Guide:Help, my tests arent running: Unterschied zwischen den Versionen

Aus TUTI
KKeine Bearbeitungszusammenfassung
(added fix to run tests via build.gradle play buttons)
 
Zeile 67: Zeile 67:
# Click <code>OK</code>.
# Click <code>OK</code>.
[[Datei:Build Gradle Use IntelliJ.png|thumb|Setting Gradle's Test Runner to IntelliJ]]
[[Datei:Build Gradle Use IntelliJ.png|thumb|Setting Gradle's Test Runner to IntelliJ]]
===5. Run via build.gradle===
# You might see a green "Play" button in your build.gradle, next to <code>sourceSets/test</code>, <code>test/useJUnitPlatform</code> or <code>dependencies</code>.
# If so, try to click one of them. This should run all of your test classes. If the tests run properly, you should also be able to run them individually again, either by rightclicking the test classes or by clicking the green arrows within them.


==C: Still not working?==
==C: Still not working?==
In case your tests still don't work, open a Zulip thread.
In case your tests still don't work, open a Zulip thread.

Aktuelle Version vom 11. Dezember 2022, 17:16 Uhr

This quick guide shall help you fix the most common JUnit errors.

It has been written for IntelliJ - might work similarly with other IDEs, but might also not.

In case your tests still don't work after going through these steps (please try to run your tests after each section and see if the issue has been resolved), open a Zulip thread.

A: Issues within the Test classes[Bearbeiten | Quelltext bearbeiten]

General things to check:

  1. Does the package name at the top of each test class correspond to the actual folder structure?
  • If a class says package pg.dp; for example, but is sitting in the folder pg, remove the .dp. If it says package pg; instead, but is sitting in pg/dp, try adding .dp to the package.
  1. Is the test trying to import libraries you need to add first?
  • Check the import section and hover over red import statements. You should get a suggestion to import the corresponding library. Click it.
  1. Are the tests relying on classes you don't have in your src/pgdp.taskname folder? Or on classes in the testfoldername folder?
  • Check if you have overlooked a class you need to implement per the import statement, or if the test was maybe written for another implementation than the one you chose. If you're trying to run student tests from the Github repo, check the corresponding pull request and see if you maybe forgot to add the missing class to your test folder. Don't shy away from asking the author if their test is correct, either.
  1. Is there a semicolon, bracket, etc missing?

B: Issues with the setup[Bearbeiten | Quelltext bearbeiten]

Assuming your folder structure looks like the image to the right, where your code is under src and test is your folder for tests:

Folder Structure Example

1. Test Sources Root[Bearbeiten | Quelltext bearbeiten]

  • Right-click on test, Mark as, Test Sources Root.
Mark Folder as Test Sources Root

2. build.gradle[Bearbeiten | Quelltext bearbeiten]

Open build.gradle.

  1. Does the file contain the name of the test folder (here: test) in single brackets under sourceSets, test, java in the array srcDirs? (1)
  • If not, copy the following at the proper location (see image):
test {
    java {
        srcDirs = ['test']
    }
}
  1. Does it contain the JUnit dependency? (2)
  • If not, copy the following at the proper location (see image). You may need to change the version number according to the JUnit version you have added.
dependencies {
    implementation 'org.junit.jupiter:junit-jupiter:5.8.1'
}
  1. Does it specify useJUnitPlatform()? (3)
  • If not, copy the following at the proper location (see image):
test{
    useJUnitPlatform()
}
How build.gradle is supposed to look

3. Project Structure[Bearbeiten | Quelltext bearbeiten]

  1. Navigate to File, Project Structure.
Navigate to Project Structure
  1. Under Modules, check that it looks like the following image:
How Modules should look

If you have a test folder on the same level as W0XH0Y, delete it by selecting it, then clicking the minus button (see the orange marking).

4. Build options[Bearbeiten | Quelltext bearbeiten]

  1. Navigate to File, Settings.
Navigate to Settings
  1. Expand (using the small arrows >) to the following level: Build, Execution, Deployment<nowiki> (1), Build Tools (2). Click Gradle (3).
  2. Under Run tests using: (4), select IntelliJ IDEA (5), if Gradle is selected.
  3. Click OK.
Setting Gradle's Test Runner to IntelliJ

5. Run via build.gradle[Bearbeiten | Quelltext bearbeiten]

  1. You might see a green "Play" button in your build.gradle, next to sourceSets/test, test/useJUnitPlatform or dependencies.
  2. If so, try to click one of them. This should run all of your test classes. If the tests run properly, you should also be able to run them individually again, either by rightclicking the test classes or by clicking the green arrows within them.

C: Still not working?[Bearbeiten | Quelltext bearbeiten]

In case your tests still don't work, open a Zulip thread.