如何测试对私有函数的访问?

时间:2016-03-03 14:33:54

标签: c++ unit-testing testing

我想知道如何测试对私有函数的访问。例如:

class Random{
    public:
        ...
    private:
        Serial();
        OR
        random_function();

在这种情况下,无法实例化该类的对象,因为Serial是私有的。

如果只有random_function()函数并且我们有一个Serial的对象,我们就无法访问object.random_function()这样的函数。

但是如果你考虑一个小组正在处理一个项目的情况,并且有一些方法应该显而易见private ......

在编写测试用例时(用C ++编写)怎么可能会这么看?

编辑: 我在寻找的是一个可以测试访问权限的测试场景: 如果将功能更改为具有公共访问权限,则测试失败。

2 个答案:

答案 0 :(得分:3)

来自here

的回答
#include <iostream>

class Random1
{
public:
    int random_function() { return 0; }
};

class Random2
{
private:
    int random_function() { return 0; }
};

// SFINAE test
template <typename T>
class has_random_function
{
    typedef char one;
    typedef long two;

    template <typename C> static one test( typeof(&C::random_function) ) ;
    template <typename C> static two test(...);    

public:
    enum { value = sizeof(test<T>(0)) == sizeof(char) };
};

int main(int argc, char *argv[])
{
    std::cout << "Random1: " << has_random_function<Random1>::value << std::endl;
    std::cout << "Random2: " << has_random_function<Random2>::value << std::endl;
    return 0;
}

答案 1 :(得分:2)

诀窍是使用SFINAE:编写一对函数,一个函数定义Random::random_functionpublic,还有一个函数定义Random::random_functionprivate <Controls:MetroWindow x:Name="wdw_MainWindow" x:Class="AdminProgram.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:Controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro" xmlns:winformchart="clr-namespace:System.Windows.Forms.DataVisualization.Charting;assembly=System.Windows.Forms.DataVisualization" xmlns:local="clr-namespace:AdminProgram" mc:Ignorable="d" Title="MainWindow" Height="600" Width="1024" GlowBrush="{DynamicResource AccentColorBrush}" WindowStartupLocation="CenterScreen" ResizeMode="NoResize" BorderThickness="2,2,0,2" ShowMinButton="False" ShowMaxRestoreButton="False" IsMinButtonEnabled="False" IsMaxRestoreButtonEnabled="False" BorderBrush="#FF7C7C7C" TitleForeground="White"> <Controls:MetroWindow.Flyouts> <Controls:FlyoutsControl> <Controls:Flyout x:Name="fyo_Menu" Header="Menu" Width="200" Theme="Accent"> <Grid> <Controls:Tile x:Name="btn_AddNew" Title="Add New" Width="150" Height="150" TitleFontSize="20" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0" Click="btn_AddNew_Click" KeepDragging="False" MouseEnter="Tile_MouseEnter" MouseLeave="Tile_MouseLeave" BorderBrush="#FFC89632"> </Controls:Tile> <Controls:Tile x:Name="btn_ViewAll" Title="View All" Width="150" Height="150" TitleFontSize="20" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="0,160,0,0" Padding="0" Click="btn_ViewAll_Click" MouseEnter="Tile_MouseEnter" MouseLeave="Tile_MouseLeave" BorderBrush="#FFC89632" > </Controls:Tile> </Grid> </Controls:Flyout> </Controls:FlyoutsControl> </Controls:MetroWindow.Flyouts> <GroupBox x:Name="gpb_Home_Stats" Header="Latest information and statistics" Margin="0,50,0,0"> <Grid> <!--- Winforms Integrated charting --> <!--Strength bars --> <WindowsFormsHost x:Name="wfh_Statistics_Strengthometer" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,0,0,0" Width="950" Height="425"> <winformchart:Chart x:Name="chart_Strengthometer" Dock="None"> <winformchart:Chart.Legends> <winformchart:Legend Docking="Left" TitleSeparator="Line" Title="Coffee count by strength"/> </winformchart:Chart.Legends> <winformchart:Chart.Series> <winformchart:Series Name="Strength" ChartType="Column"/> </winformchart:Chart.Series> <winformchart:Chart.ChartAreas> <winformchart:ChartArea/> </winformchart:Chart.ChartAreas> </winformchart:Chart> </WindowsFormsHost> </Grid> </GroupBox>

根据您的描述,第一个函数导致测试失败,第二个函数导致测试通过,但是在测试相反的情况时,您显然可以反转该逻辑。