测试工程如何搭建?配置文件如何读取?

配置文件如何读取?

  • 资源文件一般放在resources文件夹下,在pom文件中要指定资源位置,具体为下的子标签,这样编译的时候会复制到target/classes中,否则编译可能不会复制到classpath中。
  • 配置应该是单例模式,尤其是静态的配置。

1、java.util.Properties类:


private static Properties prop = new Properties();//1、创建java.util.Properties对象
public static String get(String key)
{    
     //2、配置文件读成流。配置文件格式:#开头是注释行,消息=消息文本
    InputStream in=Configurer.class.getResourceAsStream("/conf.properties"); 
    try{
prop.load(in);//3、加载配置文件
in.close();
}
catch (IOException e){
        e.printStackTrace();
}
return prop.get(key)==null?null:(String)prop.get(key);//4、读取配置文件
}

2、springframework===单例模式

importorg.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;

@PropertySource(value = "classpath:server.properties")//配置文件路径,格式为:#开头是注释行,消息=消息文本
public class AppConfigure {

@Value("${apptoken}")//这行会将配置文件中的apptoken消息文本赋值给下面的变量apptoken

public String apptoken;

    static private AppConfigure configure = null;

@Bean//将被加载为一个实例
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
return new PropertySourcesPlaceholderConfigurer();
}

public static synchronized AppConfigure get() {
if (configure == null) {   //单例模式
try {
                AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(AppConfigure.class);
configure = ctx.getBean(AppConfigure.class);
} catch (Exception e) {
logger.info(e.getMessage());
}
        }
return configure;
}

}

有多套环境的项目如何组织测试代码的结构?

stone有测试,预发,线上三套环境,如何组织测试代码?
maven和testng如何传递参数到代码中?

第一种方法

不同环境的配置参数,如url等,写到maven的profile标签里。

    <profiles>
    <profile>
        <id>project-test</id>
        <activation>
            <property>
                <name>env</name>
                <value>project-test</value>
            </property>
        </activation>
        <properties>
            <url>your-url</url>
            <testng.file>testngxml/test-testng.xml</testng.file>
        </properties>
    </profile>
  <profiles>

在配置文件conf.properties中使用参数中的变量,格式为:${标签}。
conf.properties配置文件里面,把不同环境相同的配置写在这里。

url=${url}

然后可以在代码中读取配置文件了。

使用命令选择不同的profile:mvn clean test -Denv=project-test

原理为何?如何直接在代码中使用?

第二种方法

在pom.xml文件中,每一种环境建一个profile,指定构建的资源目录:

    <profiles>
        <profile>
            <id>test</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <build>
                <resources>
                    <resource>
                        <directory>src/main/resources/test</directory>
                    </resource>
                    <resource>
                        <directory>src/main/resources/common</directory>
                    </resource>
                </resources>
            </build>
        </profile>
    <profiles>

把不同环境的配置文件放到不同的资源目录下:

image
使用命令行启动该profile:mvn test -P test
-P指profile,后面是该profile的id

第三种方法

在mvn命令中指定配置文件

在pom.xml中设置一个属性标签,如下面的mavn.filepath:
image

后面在执行mvn命令的时候,带上这个参数: mvn test -Dmaven.filepath=testng.xml

各属性节点的值,用占位符”${属性名}”占位,maven在运行的时候,会自动替换这些占位符为实际属性值。

mvn test 会去执行那个testng.xml?

在surefire插件中指定testng.xml。

<plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19.1</version>
        <configuration>
            <argLine>-Dfile.encoding=UTF-8</argLine>
            <!-- Suite testng xml file to consider for test execution -->
            <suiteXmlFiles>
                <suiteXmlFile>testng.xml</suiteXmlFile>
            </suiteXmlFiles>
        </configuration>
</plugin>

如果每个环境使用各自的testng.xml,如何配置呢?在每个profile中设置同一个变量,如:
<testng.file>testngxml/project-test.xml</testng.file>
然后在surefire插件里面的配置为:
<suiteXmlFile> ${testng.file} </suiteXmlFile>

testng中如何传递参数?

一般不使用testng去配置不同环境的参数,maven足矣!

可以在testng.xml的 或者标签下声明参数,格式:

<parameter name="first_para"  value="duan"/>
<parameter name="second_para"  value="chang"/>

代码中,可以在@Test、@Before、@After等注解的的上面使用@Parameters注解:

@Parameters("first_para","second_para")
@Test
public void testMethond(String s1,String s2){
      //这里是代码,s1 = duan, s2 = chang
}

另外有个指定默认参数的注解@Optional:

@Parameters("first_para","second_para")
@Test
public void testMethond( @Optional("duanduan")String s1,String s2){
      //这里是代码,如果testng.xml中没有first_para参数,则使用@Optional提供的参数
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
纯c读写ini配置文件 用c/c++读写ini配置文件有不少第三方的开源库,如iniparser、libini、rwini、UltraLightINIParser等,但都不理想,往往代码较大、功能较弱、 接口使用不方便。尤其在大小写处理、前后空格、各种注释、跨平台换行符支持、带引号字符串处理、无section操作、原格式保持等方面存在问题。 现将本人精心制作的ini读写程序源码奉献给大家,纯c编写,简洁好用。支持windows和linux。 主要特点: 1、支持;和#注释符号,支持行尾注释。 2、支持带引号'或"成对匹配的字符串,提取时自动去引号。引号中可带其它引号或;#注释符。 3、支持无section或空section(名称为空)。 4、支持10、16、8进制数,0x开头为16进制数,0开头为8进制。 5、支持section、key或=号前后带空格。 6、支持\n、\r、\r\n或\n\r换行格式。 7、不区分section、key大小写,但写入时以新串为准,并保持其大小写。 8、新增数据时,若section存在则在该节最后一个有效数据后添加,否则在文件尾部添加。 9、支持指定key所在整行删除,即删除该键值,包括注释。 10、可自动跳过格式错误行,修改时仍然保留。 11、修改时保留原注释:包括整行注释、行尾注释(包括前面空格)。 12、修改时保留原空行。以上三点主要是尽量保留原格式。 不足之处: 1、不支持单key多value(逗号分割),只能一次性提取后自行处理。 2、不支持同名重复section和key。(重复section可视为错误,重复key则可能造成分歧) 3、不能提取所有section或key名称。 使用只需两个文件inirw.h、inirw.c,另有测试程序和工程文件,支持windows和linux。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值