2017-06-05 16:02:39 +03:00
|
|
|
# Copyright (c) 2014-present PlatformIO <contact@platformio.org>
|
2016-08-10 15:50:01 +03:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2020-06-29 20:52:15 +03:00
|
|
|
import os
|
2022-04-21 18:11:49 +03:00
|
|
|
import subprocess
|
2016-08-10 15:50:01 +03:00
|
|
|
|
2022-04-21 18:11:49 +03:00
|
|
|
from platformio.unittest.command import unittest_cmd
|
2016-08-10 15:50:01 +03:00
|
|
|
|
|
|
|
|
2022-04-21 18:11:49 +03:00
|
|
|
def test_unity_calculator():
|
|
|
|
result = subprocess.run( # pylint: disable=subprocess-run-check
|
2019-09-23 23:13:48 +03:00
|
|
|
[
|
|
|
|
"platformio",
|
|
|
|
"test",
|
|
|
|
"-d",
|
2020-06-29 20:52:15 +03:00
|
|
|
os.path.join("examples", "unit-testing", "calculator"),
|
2019-09-23 23:13:48 +03:00
|
|
|
"-e",
|
|
|
|
"native",
|
2022-04-21 18:11:49 +03:00
|
|
|
],
|
|
|
|
capture_output=True,
|
|
|
|
text=True,
|
2019-09-23 23:13:48 +03:00
|
|
|
)
|
2022-04-21 18:11:49 +03:00
|
|
|
assert result.returncode != 0
|
|
|
|
assert all(s in str(result) for s in ("PASSED", "FAILED"))
|
2020-06-11 23:53:52 +03:00
|
|
|
|
|
|
|
|
2022-04-21 18:11:49 +03:00
|
|
|
def test_unity_setup_teardown(clirunner, validate_cliresult, tmpdir):
|
2020-06-11 23:53:52 +03:00
|
|
|
project_dir = tmpdir.mkdir("project")
|
|
|
|
project_dir.join("platformio.ini").write(
|
|
|
|
"""
|
|
|
|
[env:native]
|
|
|
|
platform = native
|
|
|
|
"""
|
|
|
|
)
|
2022-04-21 18:11:49 +03:00
|
|
|
test_dir = project_dir.mkdir("test")
|
|
|
|
test_dir.join("test_main.c").write(
|
2020-06-11 23:53:52 +03:00
|
|
|
"""
|
2022-04-21 18:11:49 +03:00
|
|
|
#include <stdio.h>
|
2020-06-29 20:52:15 +03:00
|
|
|
#include <unity.h>
|
2022-04-21 18:11:49 +03:00
|
|
|
|
|
|
|
void setUp(){
|
|
|
|
printf("setUp called");
|
|
|
|
}
|
|
|
|
void tearDown(){
|
|
|
|
printf("tearDown called");
|
|
|
|
}
|
|
|
|
|
|
|
|
void dummy_test(void) {
|
|
|
|
TEST_ASSERT_EQUAL(1, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
2020-06-11 23:53:52 +03:00
|
|
|
UNITY_BEGIN();
|
2022-04-21 18:11:49 +03:00
|
|
|
RUN_TEST(dummy_test);
|
2020-06-11 23:53:52 +03:00
|
|
|
UNITY_END();
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
result = clirunner.invoke(
|
2022-04-21 18:11:49 +03:00
|
|
|
unittest_cmd,
|
|
|
|
["-d", str(project_dir), "-e", "native"],
|
2020-06-11 23:53:52 +03:00
|
|
|
)
|
2022-04-21 19:32:12 +03:00
|
|
|
validate_cliresult(result)
|
|
|
|
assert all(f in result.output for f in ("setUp called", "tearDown called"))
|
|
|
|
|
2020-06-11 23:53:52 +03:00
|
|
|
|
2022-04-21 19:32:12 +03:00
|
|
|
def test_crashed_program(clirunner, tmpdir):
|
|
|
|
project_dir = tmpdir.mkdir("project")
|
|
|
|
project_dir.join("platformio.ini").write(
|
2022-04-21 18:11:49 +03:00
|
|
|
"""
|
2022-04-21 19:32:12 +03:00
|
|
|
[env:native]
|
|
|
|
platform = native
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
test_dir = project_dir.mkdir("test")
|
|
|
|
test_dir.join("test_main.c").write(
|
|
|
|
"""
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unity.h>
|
|
|
|
|
|
|
|
void setUp(){
|
|
|
|
printf("setUp called");
|
|
|
|
}
|
|
|
|
void tearDown(){
|
|
|
|
printf("tearDown called");
|
|
|
|
}
|
2020-06-22 14:42:45 +03:00
|
|
|
|
2022-04-21 19:32:12 +03:00
|
|
|
void dummy_test(void) {
|
|
|
|
TEST_ASSERT_EQUAL(1, 1);
|
|
|
|
}
|
2020-06-22 14:42:45 +03:00
|
|
|
|
2022-04-21 19:32:12 +03:00
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
printf("Address boundary error is %s", argv[-1]);
|
|
|
|
UNITY_BEGIN();
|
|
|
|
RUN_TEST(dummy_test);
|
|
|
|
UNITY_END();
|
|
|
|
return 0;
|
2022-04-21 18:11:49 +03:00
|
|
|
}
|
|
|
|
"""
|
|
|
|
)
|
2022-04-21 19:32:12 +03:00
|
|
|
result = clirunner.invoke(
|
|
|
|
unittest_cmd,
|
|
|
|
["-d", str(project_dir), "-e", "native"],
|
|
|
|
)
|
|
|
|
assert result.exit_code != 0
|
|
|
|
assert any(
|
|
|
|
s in result.output for s in ("Program received signal", "Program errored with")
|
|
|
|
)
|
2020-06-22 14:42:45 +03:00
|
|
|
|
2022-04-21 18:11:49 +03:00
|
|
|
|
|
|
|
def test_legacy_unity_custom_transport(clirunner, validate_cliresult, tmpdir):
|
2020-06-22 14:42:45 +03:00
|
|
|
project_dir = tmpdir.mkdir("project")
|
|
|
|
project_dir.join("platformio.ini").write(
|
|
|
|
"""
|
|
|
|
[env:embedded]
|
|
|
|
platform = ststm32
|
|
|
|
framework = stm32cube
|
|
|
|
board = nucleo_f401re
|
|
|
|
test_transport = custom
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
|
|
|
|
test_dir = project_dir.mkdir("test")
|
|
|
|
test_dir.join("test_main.c").write(
|
|
|
|
"""
|
|
|
|
#include <unity.h>
|
|
|
|
|
|
|
|
void dummy_test(void) {
|
|
|
|
TEST_ASSERT_EQUAL(1, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
UNITY_BEGIN();
|
|
|
|
RUN_TEST(dummy_test);
|
|
|
|
UNITY_END();
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
test_dir.join("unittest_transport.h").write(
|
|
|
|
"""
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void unittest_uart_begin(){}
|
|
|
|
void unittest_uart_putchar(char c){}
|
|
|
|
void unittest_uart_flush(){}
|
|
|
|
void unittest_uart_end(){}
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
"""
|
|
|
|
)
|
2022-04-21 18:11:49 +03:00
|
|
|
result = clirunner.invoke(
|
|
|
|
unittest_cmd,
|
2020-06-22 14:42:45 +03:00
|
|
|
[
|
|
|
|
"-d",
|
|
|
|
str(project_dir),
|
|
|
|
"--without-testing",
|
|
|
|
"--without-uploading",
|
|
|
|
],
|
|
|
|
)
|
2022-04-21 18:11:49 +03:00
|
|
|
validate_cliresult(result)
|