配置文件如何读取?
- 资源文件一般放在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>
把不同环境的配置文件放到不同的资源目录下:
使用命令行启动该profile:mvn test -P test
-P指profile,后面是该profile的id
第三种方法
在mvn命令中指定配置文件
在pom.xml中设置一个属性标签,如下面的mavn.filepath:
后面在执行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提供的参数
}