如何在TestNg XML中传递一个动态参数,我将使用某个随机库创建它?

huangapple go评论95阅读模式
英文:

How can I pass a dynamic parameter-that I will create with some random library- in testNg xml?

问题

我想从TestNG XML传递一个动态参数给测试用例,我的参数类似于:

String dynamicParameter = generateRandomStringForMail();

这是我的测试用例:

@Test()
public void customerCreatorAllProducts() throws Exception {
    setup();
    Functions.pages.LoginPage login = PageFactory.initElements(driver, Functions.pages.LoginPage.class);
    login.navigateRegisterPage().createFixedPasswordCustomerRequiredFields(dynamicParameter);
}

我还将在其他情况下使用这个参数,我如何从TestNG XML或其他方式实现这个目标?

英文:

I want to pass a dynamic parameter to a test case from testng xml, my parameter is something like:

String dynamicParameter=generateRandomStringForMail();

Here is my testcase:

   @Test()
    public void customerCreatorAllProducts () throws Exception {
            setup();
            Functions.pages.LoginPage login = PageFactory.initElements(driver, Functions.pages.LoginPage.class);
            login.navigateRegisterPage().createFixedPasswordCustomerRequiredFields(dynamicParameter);
    }

I will be using this parameter in other cases as well, how can i do this from testng.xml or with something else?

答案1

得分: 1

我对testing.xml不太熟悉,但在这种情况下,我立刻想到了Mockito:http://mockito.org。

  1. 将Mockito添加到您的项目中(例如通过build.gradle页面)。
  2. 在测试文件中添加导入语句:
import static org.mockito.Mockito.*;
  1. 在测试类中,创建一个该类的模拟对象,该类具有generateRandomStringForMail()方法。在我的当前项目中,我有以下代码:
DefaultFileService mockFileService = mock(DefaultFileService.class);
  1. 定义在这些测试条件下您希望该方法返回的内容,例如:
when(mockFileService.generateRandomFileName()).thenReturn("fileName");
  1. 每当您的测试需要使用该方法的结果时,您可以使用"fileName",因为您已经告诉测试环境返回这个值。我的项目中有一个方法用于更新与库存项相关联的图像文件,该过程包括使用DefaultFileService生成一个随机文件名,然后将图像文件和新文件名传递给DefaultFileService以保存文件到系统中。我的测试代码无法看到或猜测实际生成的文件名,但上面的when语句已经解决了这个问题,以便测试我的QuiltController类:
quiltController.update(data, mockFile);
verify(mockFileService).save(mockFile, "fileName"); // 确认使用预期参数调用了save()方法

这与您尝试做的事情非常相似,希望这可以帮助您继续探索Mockito。如果您想要进行测试,不要感到惊讶如果您需要重构一些工作以使其更具可测试性。我也这样做了,结果编写出了更好的代码。试一试吧 如何在TestNg XML中传递一个动态参数,我将使用某个随机库创建它?

英文:

I am not familiar with testing.xml, but Mockito immediately comes to mind for this: http://mockito.org.

  1. add Mockito to your project (e.g. through the build.gradle page)
  2. add the import to your test file:
import static org.mockito.Mockito.*;
  1. within your test class, create a mock of the class which has the generateRandomStringForMail() method. In my current project, I have
DefaultFileService mockFileService = mock(DefaultFileService.class);
  1. define what you want the method to return under these test conditions, e.g.
when(mockFileService.generateRandomFileName()).thenReturn("fileName");
  1. Whenever your tests need to use the result of the method in question, you can use "fileName", because you have told the test environment to give this response. My project has a method to update the image file associated with an inventory item, which process includes using the DefaultFileService to generate a random file name, then passing the image file and the new file name to the DefaultFileService to save the file in the system. My test code can't see or guess what file name would actually be produced, but my 'when' line above has resolved that problem for the purposes of testing my QuiltController class:
quiltController.update(data, mockFile);
verify(mockFileService).save(mockFile, "fileName"); // confirms the save() method was called with the expected parameters

It feels pretty similar to what you are trying to do, so hopefully that helps you proceed if you do want to explore Mockito. Don't be surprised if you need to refactor some of your work to make it more testable. I did, and have better code as a result. Give it a go 如何在TestNg XML中传递一个动态参数,我将使用某个随机库创建它?

huangapple
  • 本文由 发表于 2020年8月5日 07:49:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63256545.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定