Watin C# 类变得非常大,分解它们的最佳方法是什么

发布于 2024-12-12 06:39:35 字数 150 浏览 0 评论 0原文

在测试网站时,我最终得到了非常长的类文件,测试了网站的许多不同部分。我可以将它们分成单独的类,但随后我需要传递浏览器对象,这似乎会产生很多额外的开销。我还可以添加一个“代码文件”,我认为它可以包含在我正在使用的主类中并让它保存函数。将大的 C# 类文件分解为较小的文件的正确方法是什么?

In testing a site, I end up getting really long class files testing lots of different parts of a website. I could break them into separate classes but then I would need to pass around Browser Objects and seems like lots of extra overhead. I could also add a "code file" which I think could just be included in the main class I'm working in and have it hold functions. Whats the right way to break up a big c# class file into smaller files?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦里人 2024-12-19 06:39:35

我会将它们分成按类似功能分组的文件,并使用 部分关键字。

另外 -

如果你的班级变得那么大,我怀疑你给它承担了太多的责任。

I would break them into files grouped by similar functionality, and use the partial keyword.

Also -

If your class is getting that big, I suspect that you are loading it up with too many responsibilities.

洋洋洒洒 2024-12-19 06:39:35

正如您所说,您的课程“测试网站的许多不同部分”为网站的单个部分创建一个课程。
例如,您可以采取两个步骤进行重构。
1.使用partial关键字将类分成多个文件。
2. 逐步删除部分类并根据其职责重命名类名。

As you said your class "testing lots of different parts of a website" make a single class for single part of a website.
For instance you can take two steps for refactoring.
1. Break class into several files using partial keyword.
2. Step by step remove partial and rename class name accroding to its responsobility.

一身仙ぐ女味 2024-12-19 06:39:35
 Whats the right way to break up a big c# class file into smaller files?

它将它分为不同的类。

类应该有单一目的,方法应该执行单一操作,好的代码应该能够自我解释。

 Whats the right way to break up a big c# class file into smaller files?

Its to seperate it into seperate classes.

A class should have a single purpose, a method should do a single action, good code should explain itself.

别理我 2024-12-19 06:39:35

我们的项目中有超过 250 个测试。我们最初使用部分类,但发现找到一个特定的测试来运行很痛苦,因此我们将类结构化如下: -

- class Base
  - class Site1
    - class LoginTests
      - ValidLogin()
      - InvalidLogin()
    + class MyAccountTests
    + class MiscTests
  - class Site2
    + class LoginTests
    + class MyAccountTests
    + class MiscTests
  - class Site3
    + class LoginTests
    + class MyAccountTests
    + class MiscTests

然后,在 VS 的测试视图窗口中按测试名称对它们进行分组,这将向您显示...

- Site1.LoginTests
  - ValidLogin
  - InvalidLogin
+ Site1.MyAccountTests
+ Site1.MiscTests
+ Site2.LoginTests
+ Site2.MyAccountTests
+ Site2.MiscTests
+ Site3.LoginTests
+ Site3.MyAccountTests
+ Site3.MiscTests

We have over 250 tests in our project. We initially used partial classes but found finding a specific test to run a pain, so we've structured the classes like this: -

- class Base
  - class Site1
    - class LoginTests
      - ValidLogin()
      - InvalidLogin()
    + class MyAccountTests
    + class MiscTests
  - class Site2
    + class LoginTests
    + class MyAccountTests
    + class MiscTests
  - class Site3
    + class LoginTests
    + class MyAccountTests
    + class MiscTests

Then, in your Test View window in VS group them by Test Name which will show you...

- Site1.LoginTests
  - ValidLogin
  - InvalidLogin
+ Site1.MyAccountTests
+ Site1.MiscTests
+ Site2.LoginTests
+ Site2.MyAccountTests
+ Site2.MiscTests
+ Site3.LoginTests
+ Site3.MyAccountTests
+ Site3.MiscTests
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文