我正在尝试官方文档中的 Django 测试装置,但我的测试类找不到assertContains。
from django.utils import unittest
from django.test import Client
class SimpleTest(unittest.TestCase):
def setUp(self):
self.client = Client()
def test_details(self):
response = self.client.post('/register',
{'username': '123',
'password': '123',
follow=True)
self.assertEqual(response.status_code, 200)
self.assertContains(response, "Logout")
self.assertNotContains(response, "Login")
Run Code Online (Sandbox Code Playgroud) 我有两个按钮.如果一个按钮的类禁用,则其他按钮没有.现在我想检查一下,如果第一个按钮的类禁用,我需要点击第二个按钮,反之亦然.
我正在尝试下面的代码,但它每次都返回true.根据条件动态添加禁用类.
if (expect(element(by.css('ul#menu [data-selector="holdInventory"]'))
.getAttribute('class')).toEqual('disabled')) {
console.log('has class');
} else {
console.log('has not class');
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
我写了以下代码:
#include <stdio.h>
int array[] = {23, 43, 12, 17, 204, 99, 16};
#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int main()
{
int test = -1;
if (test <= TOTAL_ELEMENTS)
{
printf("Hello, got here!\n");
}
}
Run Code Online (Sandbox Code Playgroud)
当我编译此代码(带有gcc main.c -Wall(无警告!))并运行它时,printf无法执行。我的意思是,test= -1,它肯定小于数组的大小(7 位数字)。错误在哪里?
测试计划、测试套件、测试用例和测试场景之间有什么区别?质量保证团队是否遵循任何格式或可遵循的通用格式?
我想测试下面的java程序.
package com.arrays;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* Find all pairs in an array of non-negative unique numbers whose sum is equal to k
* For Example. {1,3,4,5,6} and k=9, then {3,6} and {4,5}
*
*/
public class FindAllPairsIfSumToK {
public List<Pair> findAllPairsWhoseSumIsK(int[] inputArray, int k) {
Set<Integer> tempSet = new HashSet<>();
List<Pair> pairs = new ArrayList<>();
for(int i=0;i<inputArray.length;i++) {
tempSet.add(inputArray[i]);
}
for(int i=0;i<inputArray.length;i++) {
if((2*inputArray[i] != k) && tempSet.contains(k-inputArray[i])) {
pairs.add(new Pair(inputArray[i], k-inputArray[i]));
} …Run Code Online (Sandbox Code Playgroud) 我目前正在使用 Spring boot test 测试我的一项服务。该服务导出所有用户数据并在成功完成后生成 CSV 或 PDF。在浏览器中下载文件。
以下是我在测试类中编写的代码
MvcResult result = MockMvc.perform(post("/api/user-accounts/export").param("query","id=='123'")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.accept(MediaType.APPLICATION_PDF_VALUE)
.content(TestUtil.convertObjectToJsonBytes(userObjectDTO)))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_PDF_VALUE))
.andReturn();
String content = result.getResponse().getContentAsString(); // verify the response string.
Run Code Online (Sandbox Code Playgroud)
下面是我的资源类代码(调用到这个地方)-
@PostMapping("/user-accounts/export")
@Timed
public ResponseEntity<byte[]> exportAllUsers(@RequestParam Optional<String> query, @ApiParam Pageable pageable,
@RequestBody UserObjectDTO userObjectDTO) {
HttpHeaders headers = new HttpHeaders();
.
.
.
return new ResponseEntity<>(outputContents, headers, HttpStatus.OK);
}
Run Code Online (Sandbox Code Playgroud)
当我调试我的服务并在退出之前放置调试时,我得到的内容类型为“应用程序/pdf”,状态为 200。我试图在我的测试用例中复制相同的内容类型。不知何故,它在执行过程中总是抛出以下错误 -
java.lang.AssertionError: Status
Expected :200
Actual :406
Run Code Online (Sandbox Code Playgroud)
我想知道,我应该如何检查我的响应(ResponseEntity)。还有什么应该是响应所需的内容类型。
我刚刚从File-> New-> JUnit Test Cases创建了一个测试类,这是我的整个代码:
import static org.junit.jupiter.api.Assertions.*;
import org.junit.After;
import org.junit.Before;
import org.junit.jupiter.api.Test;
class TestingJUnit {
@Before
public void testOpenBrowser() {
System.out.println("Opening Chrome browser");
}
@Test
public void tesingNavigation() {
System.out.println("Opening website");
}
@Test
public void testLoginDetails() {
System.out.println("Enter Login details");
}
@After
public void testClosingBrowser() {
System.out.println("Closing Google Chrome browser");
}
}
Run Code Online (Sandbox Code Playgroud)
但是,当我运行此cas时,只有@Test批注正在运行@Before和@After批注代码未运行。请指导我,我认为这是JUnit版本或Eclipse IDE版本的问题,而且我的所有Test类都未在TestSuit中运行。我不知道为什么,尽管Test Classes可以正常运行。我的TestSuit类在这里:
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(Suite.class)
@SuiteClasses({TestingJUnit.class, SecondTest.class})
public class TestSuit {
}
Run Code Online (Sandbox Code Playgroud) 我使用的是 django 3.2,测试包中的 self.client.force_login 无法登录。这是设置方法:
def setUp(self) -> None:
self.user = User.objects.create_user(
email='asdf@gmail.com',
password='hiwa_asdf',
name='smile as we go ahead'
)
self.client.force_login(self.user)
Run Code Online (Sandbox Code Playgroud)
这里是测试方法:
def test_update_uer_profile(self):
"""testing if update user profile works"""
data = {
'name': 'Angry as hell',
'password': 'hi there john'
}
response = self.client.patch(me_url, data)
print(response.content)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.user.refresh_from_db()
self.assertEqual(self.user.name, data['name'])
self.assertTrue(self.user.check_password(data['password']))
Run Code Online (Sandbox Code Playgroud)
结果如下:
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
b'{"detail":"Authentication credentials were not provided."}'
F
======================================================================
FAIL: test_update_uer_profile (user.tests.test_user_api.PrivateApiTest)
testing if update …Run Code Online (Sandbox Code Playgroud) 这是我试图解决的问题,以及测试我遇到问题的测试用例的解决方案。
#include <iostream>
#include <vector>
#include <unordered_map>
class Solution {
public:
int longestConsecutive(std::vector<int>& nums)
{
std::unordered_map<int, int> seq;
for (auto i : nums)
{
++seq[i];
}
int max{ 0 };
for (auto i : seq)
{
int ans{ 0 };
int x{ i.first };
if (seq[x] > 0)
{
ans = 1;
int p{ x };
int q{ x };
while (seq[p - 1] > 0)
{
--p;
++ans;
}
while (seq[q + 1] > 0)
{
++q;
++ans; …Run Code Online (Sandbox Code Playgroud) 我需要帮助为此代码创建JUnit 4测试用例.
public static int computeValue(int x, int y, int z)
{
int value = 0;
if (x == y)
value = x + 1;
else if ((x > y) && (z == 0))
value = y + 2;
else
value = z;
return value;
}
Run Code Online (Sandbox Code Playgroud)
编辑
我想要这样的东西测试if else语句
public class TestingTest {
@Test
public void testComputeValueTCXX() {
}
…
@Test
public void testComputeValueTCXX() {
}
}
Run Code Online (Sandbox Code Playgroud) 我的需求:
使用 while 循环迎接尽可能多的名称在标准输入中可用。当您将字符串 '42' 作为名称读取时停止。
我的编码:
#include<iostream>
using namespace std;
int main()
{
int input=1;
int i= 0;
string name;
while(input<=i)
{
cin>>name;
if(name=="42")
{
break;
}
else
{
cout<<"Hello "<<name<<"!";
i++;
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
结果:
对于输入 42,测试用例通过。对于其他输入,测试用例失败。请发布您的答案。
大约 1 年后回答:
非常抱歉这个问题。这是我在对 C++ 了解 0 时问的问题。这可能对新生有用。
testcase ×12
java ×3
testing ×3
c++ ×2
django ×2
unit-testing ×2
angularjs ×1
c ×1
compilation ×1
if-statement ×1
jasmine ×1
junit ×1
junit4 ×1
junit5 ×1
linked-list ×1
protractor ×1
python ×1
spring ×1
spring-boot ×1
stack ×1
test-plan ×1
test-suite ×1
while-loop ×1