针对pytest:按特定顺序运行测试模块和pytest顺序执行这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展c#–按特定顺序分配资金、Java如何在JUnit4中按特定顺序运行测试方法?、
针对pytest:按特定顺序运行测试模块和pytest 顺序执行这两个问题,本篇文章进行了详细的解答,同时本文还将给你拓展c# – 按特定顺序分配资金、Java如何在JUnit4中按特定顺序运行测试方法?、linux – 如何按特定顺序停止systemd服务、objective-c – 在XCode 8中,在给定的XCTestCase类中以特定顺序运行测试方法的方法是什么?等相关知识,希望可以帮助到你。
本文目录一览:- pytest:按特定顺序运行测试模块(pytest 顺序执行)
- c# – 按特定顺序分配资金
- Java如何在JUnit4中按特定顺序运行测试方法?
- linux – 如何按特定顺序停止systemd服务
- objective-c – 在XCode 8中,在给定的XCTestCase类中以特定顺序运行测试方法的方法是什么?
pytest:按特定顺序运行测试模块(pytest 顺序执行)
如何解决pytest:按特定顺序运行测试模块?
让我说我对单元测试和使用 pytest
还是个新手。
我正在构建单元测试并使用 GitHub Actions 运行它们,我了解它失败的原因。我需要按特定顺序运行测试模块。
作为参考,我有以下 repo 结构(为简单起见进行了概括)
mypackage/
├── __init__.py
├── foo.py
└── bar.py
tests/
├── __init__.py
├── test_foo.py
└── test_bar.py
我的问题(为概括而编写):有没有办法用 pytest
按照给定的顺序运行特定的测试模块(不是函数)?默认情况下,pytest
将按字母顺序运行,即 test_bar
在 test_foo
之前。但是,就我而言,test_bar
使用来自 test_foo
的数据,因此我希望翻转订单。我可以找到其他解决方法,但从我的搜索中,我只找到了对 given 模块中特定功能订单使用 pytest-order
或 pytest-ordering
的解释。我认为这对于端到端测试来说是一个很好的问题,同时仍能确保 100% 的代码覆盖率。
谢谢。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
c# – 按特定顺序分配资金
例如
a $100 b $200 c -$200 d -$200 e $500
应付总金额是所有项目的总和,在这种情况下为400美元
问题是我必须调用第三方系统逐个分配这些金额.但在分配期间,我不能让余额低于$0或高于总金额($400).
因此,如果我按上述顺序插入a,b,c将起作用,因此当前分配的总和= 100 200 – 200 = $100.
但是,当我尝试分配d.系统将尝试添加 – $200,这将使当前分配的金额 – $100,即< $0是不允许的,所以系统会拒绝它. 如果我对列表进行排序,那么负面项目是最后的.即
a $100 b $200 e $500 c -$200 d -$200
a将工作,b将工作,但当它试图插入e时,将有不足的资金错误,因为我们已超过400美元的最大值.我已经意识到没有银弹,并且总会有一些场景会破坏.但是我想提出一个大部分时间都可以工作的解决方案.
正常的数据样本将包含5到100个项目.只有2-15%的含量为负数.
有没有一种聪明的方法可以对列表进行排序?或者只是多次尝试分配会更好.例如,将正面和负面分成两个列表.插入正数直到一个错误,然后插入负数直到它出错,然后在列表之间来回切换,直到全部分配或直到它们都出错.
解决方法
#include <iostream> #include <vector> #include <numeric> #include <algorithm> using namespace std; vector<int> orderTransactions(const vector<int>& input) { int max = accumulate(input.begin(),input.end(),0); vector<int> results; // if the sum is negative or zero there are no transactions that can be added if (max <= 0) { return results; } // split the input into positives and negatives vector<int> sorted = vector<int>(input); sort(sorted.begin(),sorted.end()); vector<int> positives; vector<int> negatives; for (int i = 0; i < sorted.size(); i++) { if (sorted[i] >= 0) { positives.push_back(sorted[i]); } else { negatives.push_back(sorted[i]); } } // try to process all the transactions int sum = 0; while (!positives.empty() || !negatives.empty()) { // find the largest positive transaction that can be added without exceeding the max bool positiveFound = false; for (int i = (int)positives.size()-1; i >= 0; i--) { int n = positives[i]; if ((sum + n) <= max) { sum += n; results.push_back(n); positives.erase(positives.begin()+i); positiveFound = true; break; } } if (positiveFound == true) { continue; } // if there is no positive find the smallest negative transaction that keep the sum above 0 bool negativeFound = false; for (int i = (int)negatives.size()-1; i >= 0; i--) { int n = negatives[i]; if ((sum + n) >= 0) { sum += n; results.push_back(n); negatives.erase(negatives.begin()+i); negativeFound = true; break; } } // if there is neither then this as far as we can go without splitting the transactions if (!negativeFound) { return results; } } return results; } int main(int argc,const char * argv[]) { vector<int> quantities; quantities.push_back(-304); quantities.push_back(-154); quantities.push_back(-491); quantities.push_back(-132); quantities.push_back(276); quantities.push_back(-393); quantities.push_back(136); quantities.push_back(172); quantities.push_back(589); quantities.push_back(-131); quantities.push_back(-331); quantities.push_back(-142); quantities.push_back(321); quantities.push_back(705); quantities.push_back(210); quantities.push_back(731); quantities.push_back(92); quantities.push_back(-90); vector<int> results = orderTransactions(quantities); if (results.size() != quantities.size()) { cout << "ERROR: Couldn't find a complete ordering for the transactions. This is as far as we got:" << endl; } for (int i = 0; i < results.size(); i++) { cout << results[i] << endl; } return 0; }
Java如何在JUnit4中按特定顺序运行测试方法?
如何解决Java如何在JUnit4中按特定顺序运行测试方法??
我认为这对于JUnit来说是非常重要的功能,如果JUnit的作者不希望使用订购功能,为什么呢?
我不确定用JUnit做到这一点的干净方法,据我所知JUnit假定所有测试都可以按任意顺序执行。从常见问题解答:
如何使用测试治具?
(…)测试方法调用的顺序不能保证,因此testOneItemCollection()
可能在testemptyCollection()
之前执行。(...)
为什么会这样呢?好吧,我相信使测试顺序依存性是作者不希望提倡的做法。测试应该是独立的,他们不应该被耦合,并且违反本将让事情变得难以维持,将打破单独(明显),运行等测试的能力
话虽如此,如果你真的想朝这个方向发展,请考虑使用TestNG,因为它支持以本机任意顺序运行测试方法(以及指定方法取决于方法组的事情)。Cedric Beust解释了如何按照testng中的测试执行顺序执行此操作。
解决方法
我想执行@Test
以特定顺序注释的测试方法。
例如:
public class MyTest {
@Test public void test1(){}
@Test public void test2(){}
}
我想确保每次运行test1()
前test2()
都运行MyTest
,但是找不到类似的注释@Test(order=xx)
。
我认为对于JUnit来说这是非常重要的功能,如果JUnit的作者不希望使用订单功能,为什么呢?
linux – 如何按特定顺序停止systemd服务
设置启动依赖项非常简单,但我无法弄清楚如何确保在挂载服务停止之前服务已停止.
mountCustomPartitions.service
[Unit] Description=My Custom Partition Mounting Service [Service] Type=oneshot RemainAfterExit=yes ExecStart=/usr/bin/mountCustomPartitions.sh mount ExecStop=/usr/bin/mountCustomPartitions.sh unmount [Install] WantedBy=multi-user.target
ProgramA.service
[Unit] Description=My Generic Program A Service Wants=mountCustomPartitions.service After=mountCustomPartitions.service [Service] Type=simple ExecStart=/usr/bin/ProgramA [Install] WantedBy=multi-user.target
ProgramB.service
[Unit] Description=My Generic Program B Service Requires=ProgramA.service Wants=mountCustomPartitions.service After=mountCustomPartitions.service ProgramA.service [Service] Type=simple ExecStart=/usr/bin/ProgramB [Install] WantedBy=multi-user.target
在上面的场景中,mountCustomPartitions.service必须在程序服务之前启动,但也必须在它们之后停止.如果mountCustomPartitions.service被显式停止,那么它应该导致其他人也停止(但必须等待它们停止).我还需要确保ProgramB在ProgramA之后启动,但也在ProgramA之前停止.希望这不是太混乱.
我能想到的唯一解决方案是让每个服务都有一个ExecStop行,为特定服务执行systemctl stop [service]命令,该服务必须在停止之前停止.我遇到的问题是我实际上目前有六个服务使用已安装的分区,必须在尝试卸载之前停止.在这六个中,有些需要按特定顺序停止.由于这是在商业产品中使用,我希望有一个更清洁的解决方案.
解决方法
以下是official docs对此的评价:
…when two units with an ordering dependency between them are shut down,the inverse of the start-up order is applied. i.e. if a unit is configured with After= on another unit,the former is stopped before the latter if both are shut down…
objective-c – 在XCode 8中,在给定的XCTestCase类中以特定顺序运行测试方法的方法是什么?
但是在XCode 8中,情况已不再如此.例如,我将首先运行名为test1,test2,test3,test4和test4的测试方法(参见下面的屏幕截图).然后我可以重新运行,test2将在下一次运行时首先运行.
那么……如何在XCode 8上按顺序运行测试…?
解决方法
问题回顾:
我需要连续运行几个测试:test1,test4.每个测试都建立了一个期望,测试的最后一步将满足预期,然后测试将结束,下一个测试将运行.
但是在XCode 8中,测试现在以随机顺序运行.虽然这是好的,但是,如果它们是单元测试,它们应该能够以随机顺序运行,如果它们不是作为单元测试而是作为端到端测试而设计,则会破坏您的测试.
例如,在我的情况下它打破了测试,因为在第一次测试中,用户登录并设置一些数据等.然后,第二个测试检查数学,然后第三个测试将数据同步到服务器,然后第四个测试将其全部删除并从服务器向下同步.当第一个测试运行时,在构建时,shell脚本从MSYQL文件进入服务器的DB,然后在应用程序启动时,AppDelegate为应用程序安装一个新的CoreData DB.因此,如果我必须在每次测试后重新启动应用程序,那么shell脚本将重新启动服务器数据库并导致应用程序的本地CoreData数据库也重新初始化.这将打破后续测试(它是一个端到端测试,后续测试依赖于应用程序和服务器的状态,在上一次测试运行后以某种方式).
而不是设置四个不同的CoreData启动数据库和四个不同的服务器初始化脚本(这将是一个巨大的痛苦,并使得每当我们有架构更改时管理的端到端测试成倍地耗费时间),或者必须记住运行而不是连续手动构建每个测试,而是使用以下策略将所有四种测试方法合并为一个非常长的测试方法.
解
首先,在XCTestCase类中,我设置了一个测试期望属性:
@property (nonatomic,strong) XCTestExpectation *endOfTestExpectation;
在我的XCTestCase类的test1结束时,我用这个新的期望替换了它的现有期望,如下所示:
self.endOfTestExpectation = [self expectationWithDescription: @"endOfTestExpectation"]; [self waitForExpectationsWithTimeout:900 handler:^(NSError * _Nullable error) { /* Code moved from test4's expectation completion block goes here */ }
对于每个test1到test3,我将测试原始期望完成块内的代码移动到一个名为completion1到completion3的新方法中.对于test4,我将其原始期望完成块内的代码移动到test1方法结束时的endOfTestExpectation的完成块中.
然后我将方法test2重命名为test4,命名为t3st2到t3st4(快速而肮脏,我知道;你应该选择更具描述性的东西).在completion1方法结束时,我调用t3st2;在完成2结束时我打电话给t3st3;在完成3结束时我打电话给t3st4;在完成4结束时,我打电话给[self.endOfTestExpectation履行];.
这实际上最终比旧的方式更好,因为以旧方式,即使第一次测试失败,后续测试仍将运行!现在,无论XCTFail发生在哪里,整个事情就会停止,如果我被标记为SO:D,我们就不会浪费时间来运行其余的事情.
今天关于pytest:按特定顺序运行测试模块和pytest 顺序执行的讲解已经结束,谢谢您的阅读,如果想了解更多关于c# – 按特定顺序分配资金、Java如何在JUnit4中按特定顺序运行测试方法?、linux – 如何按特定顺序停止systemd服务、objective-c – 在XCode 8中,在给定的XCTestCase类中以特定顺序运行测试方法的方法是什么?的相关知识,请在本站搜索。
本文标签: