Test with JSON output

Hello,

I’m relatively new to the platformio world, and I’m struggling to build some unit tests for my esp8266 code.

My intention is to build several sets of test cases, in order to exercise part of my code before deployment on hardware.

In order to review the tests, I need to use the json output from the test, and here is where I’m struggling to:

my platformio.ini is

[platformio]

default_envs = native, esp8266, unity

…

[env:unity]

test_framework = unity

platform = native

lib_ldf_mode = deep+

test_build_src = false

test_filter = test_unity

;test_testing_command =   

;  pio test     

;  --environment ${this.__env__}     

;  --json-output-path=/test/logs 

;  --verbose

build_flags =

  -g

  -O0

  -v

  -Itest/native

  -Itest/include

  -Ilib

  -Iinclude

  -Iinclude/interfaces

  -Iinclude/templates

  -std=c++17

  -mconsole

  -Wl,--subsystem,console

And my test code, for this test, is pretty simple, just to exercise the pio test command and make sure all parameters are correct.

[project dir]/test

├───include
│       json.hpp
│       
├───logs
│
└───test_unity
    │   test_main.cpp
    │
    └───Basic_Test
            Basic_Test_010.cpp

What I’m struggling is to define a proper platformio.ini so executing

pio test --environment unity

generates a json log in /test/logs.

I tried several combinations, last of all uncommenting these lines in platformio.ini

;test_testing_command =   

;  pio test     

;  --environment ${this.__env__}     

;  --json-output-path=/test/logs 

;  --verbose

without success.

Please help. I want to avoid as much as possible to add –json-output-path to my command line.

thanks,

I actually don’t see a way to force the regular “pio test” command to use JSON output from within the platformio.ini.

I think the best you can do is create a new project task, that will show up in VSCode, too.

In the platformio.ini:

extra_scripts = test_json.py

test_json.py:

Import("env")
env.AddCustomTarget(
    name="testjson",
    dependencies=None,
    actions=[
        "pio test --json-output-path=/test/logs -v -e \"" + env["PIOENV"] + "\"",
    ],
    title="Test (JSON)",
    description="Run unit tests with JSON output"
)

This is then also executable as pio run -t testjson.

thanks, I introduced a small change to your script

Import("env")
env.AddCustomTarget(
    name="testjson",
    dependencies=None,
    actions=[
        "pio test --json-output-path=test/logs -v -e \"" + env["PIOENV"] + "\"",
    ],
    title="Test (JSON)",
    description="Run unit tests with JSON output"
)
test/logs to test/logs

to make sure the directory that is picked is relative to the project (my original intent).

it works.