patrol icon indicating copy to clipboard operation
patrol copied to clipboard

Debugging code in tests?

Open itsJoKr opened this issue 1 year ago • 7 comments

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 avatar Oct 06 '23 07:10 itsJoKr

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.

itsJoKr avatar Oct 06 '23 07:10 itsJoKr

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:

  • #995
  • #1233

bartekpacia avatar Oct 09 '23 14:10 bartekpacia

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.

jmartins-sh avatar Oct 23 '23 19:10 jmartins-sh

@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

itsJoKr avatar Oct 24 '23 08:10 itsJoKr

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

mishkov avatar Dec 17 '23 09:12 mishkov

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

eikebartels avatar Mar 18 '24 09:03 eikebartels

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!

famasf1 avatar Aug 06 '24 09:08 famasf1