Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debugging code in tests? #1777

Open
itsJoKr opened this issue Oct 6, 2023 · 7 comments
Open

Debugging code in tests? #1777

itsJoKr opened this issue Oct 6, 2023 · 7 comments

Comments

@itsJoKr
Copy link

itsJoKr commented Oct 6, 2023

Use case

When a test fails, it is often very beneficial to be able to set a breakpoint, run the debug mode and see what is actually happening. This is possible to do with test widgetTest.

Anyway, I'm not sure how to do that with patrol. Didn't find anything in docs or issues. Is this even possible to do?

Proposal

There are many ways to do this, but some minimal case would be a docs page that explains how to set up and run debug in tests. I would assume since it's not run from the Intellij/Studio it is possible to attach to running test after it is run.

@itsJoKr
Copy link
Author

itsJoKr commented Oct 6, 2023

After some research, yes we can wait in tests for (e.g. 15 seconds) and in that window we can run Flutter Attach and breakpoints will work.

The only issue I had was that in IntelliJ New UI Flutter Attach icon was not there (fixed it with https://stackoverflow.com/a/74728357/4110778).

So, to conclude: there is a way for us to attach and debug. It would be ideal if there's a smarter way than adding 15-second await.

If there's nothing to add we can close this issue.

@bartekpacia
Copy link
Contributor

Let's keep it open for now, every discussion is good so we can better understand your needs :) Thanks for researching this as well!

Related:

@jmartins-sh
Copy link

Oi, how you guys doing?

Hey @itsJoKr do we have to attach using the test_bundle.dart? I'm trying to debug using vscode and in the configuration file I'm setting the program file to test_bundle.dart but It seems that is not working 🥲

Does anyone has any tips? I appreciate It.

@itsJoKr
Copy link
Author

itsJoKr commented Oct 24, 2023

@joaoantoniomartinsfilho I'm not attaching to test bundle, all I do is:

  1. patrol test ...
  2. Add a 15 second delay inside the test, so you have time to attach
  3. Click Flutter attach icon in Android studio

@mishkov
Copy link

mishkov commented Dec 17, 2023

I think it would be nice to just setup launch.json file in VS code to debug any test. Some kind of this

  "configurations": [
    {
      "name": "Debug Patrol",
      "request": "launch",
      "type": "dart",
      "program": "integration_test/test_bundle.dart",
      "args": ["--flavor", "develop"]
    },

Maybe developing of VS Code extension is required to setup such launch configuration

@eikebartels
Copy link

Hello, I have implemented a workflow which works for me locally. It works like this:

  • Have the test file you want to debug open
  • Start Custom Launcher Command
  • The Launcher Command runs a custom task
  • The custom task runs a shell script which runs patrol develop and watches for certain pattern in the stdout

In launch.json:

    {
      "name": "Flutter: Debug and attach current test file",
      "type": "dart",
      "request": "attach",
      "preLaunchTask": "run-patrol-and-capture",
      "vmServiceInfoFile": "${workspaceFolder}/.run/vmServiceFileInfo.json"
    }

in tasks.json

{
      "label": "run-patrol-and-capture",
      "isBackground": true,
      "type": "shell",
      "command": "./patrol-capture.sh integration_test/${fileBasename}",
      "group": "build",
      "options": {
        "cwd": "path/to/app"
      },
      "problemMatcher": {
        "owner": "custom",
        "pattern": {
          "regexp": "^.*$", // Match any line
          "file": 1,
          "location": 2,
          "message": 3
        },
        "background": {
          "activeOnStart": true,
          "beginsPattern": ".+", // Matches any output
          "endsPattern": "^===========================================================$"
        }
      },
      "presentation": {
        "reveal": "always",
        "panel": "new"
      }
    }

and the capture script:

#!/bin/bash
testFilePath="$1"

# Assuming this script is in a subdirectory of the project root
PROJECT_ROOT="$(git rev-parse --show-toplevel)"
RUN_PATH="$PROJECT_ROOT/.run"
mkdir -p "$RUN_PATH"
VM_SERVICE_FILE_INFO="$RUN_PATH/vmServiceFileInfo.json"

# Delete the existing vmServiceFileInfo.json file if it exists
if [ -f "$VM_SERVICE_FILE_INFO" ]; then
    rm "$VM_SERVICE_FILE_INFO"
fi

patrol develop -t $testFilePath -d "iPhone 15 Pro" | while read -r line; do
  echo "$line"
  if [[ $line =~ http://127.0.0.1:[0-9]+/[A-Za-z0-9=_-]+/ ]]; then
    echo "==========================================================="
    echo "Found line with URI: $line"
    uri=${BASH_REMATCH[0]}
    echo "Captured URI: $uri"

    # Write the URI in JSON format to vmServiceFileInfo.json
    echo "{\"uri\": \"$uri\"}" > "$VM_SERVICE_FILE_INFO"

    echo "==========================================================="
  fi
done

@famasf1
Copy link

famasf1 commented Aug 6, 2024

Hello, I have implemented a workflow which works for me locally. It works like this:

  • Have the test file you want to debug open
  • Start Custom Launcher Command
  • The Launcher Command runs a custom task
  • The custom task runs a shell script which runs patrol develop and watches for certain pattern in the stdout

In launch.json:

This method works great. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants