简体   繁体   English

有没有办法在不同的单元测试中运行单元测试?

[英]Is there a way to run a unit test inside a different unit test?

I am writing some python x selenium unit tests for the login functionality of a website.我正在为网站的登录功能编写一些 python x selenium 单元测试。 I have already written a unit test for a valid login, but I want to write one for the "Remember Me" functionality.我已经为有效登录编写了一个单元测试,但我想为“记住我”功能编写一个单元测试。 I could easily just copy/paste the login unit test code into the new one, but that would make a VERY long block of code.我可以轻松地将登录单元测试代码复制/粘贴到新的代码中,但这会产生很长的代码块。 I was wondering if there was any way to utilize another unit test's code for a separate unit test in order to save some room.我想知道是否有任何方法可以将另一个单元测试的代码用于单独的单元测试以节省一些空间。

It's not about utilization unit tests.这与利用率单元测试无关。
You should write your methods / classes in a way they can be utilized easily.您应该以易于使用的方式编写方法/类。
So that your login method will call similar methods that "remember me" method uses but with appropriate changes according to scenario flow differences.这样您的登录方法将调用“记住我”方法使用的类似方法,但会根据场景流程差异进行适当的更改。

I'd split the test case in multiple methods.我会用多种方法拆分测试用例。 For example, you could have openLoginPage and loginAs(String user, String password, boolean rememberMe) and then use these methods in the individual tests.例如,您可以有openLoginPageloginAs(String user, String password, boolean rememberMe)然后在个别测试中使用这些方法。

If you keep taking this further, you'll get to Page Objects , where you hide all the details of a page (like field and button IDs) in the page object, and the test case uses high-level method calls on those page objects.如果您继续深入,您将进入Page Objects ,您可以在 object 页面中隐藏页面的所有详细信息(如字段和按钮 ID),并且测试用例对这些页面对象使用高级方法调用. Each test that needs to log in can reuse the LoginPage object.每个需要登录的测试都可以复用LoginPage object。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM