You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The sample_pkg_test_node is the reverse of the sample_pkg_node, which subscribes to sending_topic and publishes to received_topic.
sample_pkg/test/sample_pkg_test.cpp
#include<ros/ros.h>
#include<gtest/gtest.h>
#include<std_msgs/String.h>classSamplePkgTest: public ::testing::Test
{
public:SamplePkgTest()
: node_handle_(),
publisher_(node_handle_.advertise<std_msgs::String>("received_topic", 5, true)),
subscriber_(node_handle_.subscribe("sending_topic", 5, &SamplePkgTest::Callback, this)),
message_received_(false)
{
}
/* * This is necessary because it takes a while for the node under * test to start up.*/voidSetUp() override {
while (!IsNodeReady()) {
ros::spinOnce();
}
}
voidTearDown() override {
}
voidPublish(std_msgs::String& msg) {
publisher_.publish(msg);
}
/* * This is necessary because it takes time for messages from the * node under test to reach this node.*/
boost::shared_ptr<const std_msgs::String> WaitForMessage() {
// The second parameter is a timeout duration.return ros::topic::waitForMessage<std_msgs::String>(
subscriber_.getTopic(), node_handle_, ros::Duration(1));
}
private:bool message_received_;
ros::NodeHandle node_handle_;
ros::Publisher publisher_;
ros::Subscriber subscriber_;
/* * This callback is a no-op because we get the messages from the * node under test using WaitForMessage().*/voidCallback(const std_msgs::String& event) {
message_received_ = true;
}
/* * See SetUp method.*/boolIsNodeReady() {
return (publisher_.getNumSubscribers() > 0) && (subscriber_.getNumPublishers() > 0);
}
};
TEST_F(SamplePkgTest, SampleTest) {
std_msgs::String msg;
msg.data = "Test";
Publish(msg);
auto output = WaitForMessage();
ASSERT_TRUE(output != NULL);
EXPECT_EQ("Test -- output", output->data);
}
Hi, I have a question regarding branch coverage. I have turned on the branch coverage in the config file `/etc/lcovrc'
The sample code I'm testing does not have any if statement, so I was expecting the branch coverage to be 100%. But, the result is not what I expected.
For some reason, some lines that do not belong to the branches are treated as branch statements.
Here's the minimum working example.
Testing environment: Ubuntu 20.04.4 LTS, LCOV 1.14, and GCOV 9.04.
The
sample_pkg_node
subscribes to topicreceived_topic
and publishes to topicsending_topic
, both of them havingstd_msg::String
type.sample_pkg/src/sample_pkg_node.cpp
The
sample_pkg_test_node
is the reverse of thesample_pkg_node
, which subscribes tosending_topic
and publishes toreceived_topic
.sample_pkg/test/sample_pkg_test.cpp
sample_pkg/test/main_test.cpp
sample_pkg/test/sample_pkg_test.test
I have followed the README to setup the CMakeLists.txt as well as the package.xml.
sample_pkg/CMakeLists.txt
sample_pkg/package.xml
Then, the package is built by the following commands:
I don't know which part I was doing it wrong. Any insight would be appreciated. Thank you.
The text was updated successfully, but these errors were encountered: