Пример проблемы взят с Codewars.com (количество гласных в строке).
A Java класс содержит несколько методов для ее решения:
- getCountBC1 ()
- getCountBC2 ()
Класс TestNG содержит тесты для метода getCountBC1()
. Те же самые тесты могут применяться к getCountBC2()
.
Вопрос:
Как изменить класс TestNG в одном месте , чтобы вместо этого проверить getCountBC2()
?
Тест:
import org.testng.Assert;
import org.testng.annotations.Test;
public class VowelsTest {
// how to change tested method to Vowels.getCountBC2 across entire class?
@Test
public void testSomeVowels() {
Assert.assertEquals(Vowels.getCountBC1("abracadabra"), 5);
}
@Test
public void testAllVowels() {
Assert.assertEquals(Vowels.getCountBC1("aaaiiooooyyyuuu"), 12);
}
@Test
public void testZeroVowels() {
Assert.assertEquals(Vowels.getCountBC1("bbc"), 0);
}
}
Тестируемый класс:
public class Vowels {
public static int getCountBC1(String str) {
return str.replaceAll("(?i)[^aeiou]", "")
.length();
}
public static int getCountBC2(String str) {
return (int) str.chars().filter(c -> "aeiou".indexOf(c) >= 0).count();
}
}
В Python есть функция высшего порядка functools.partial()
. В этом примере тестовый класс будет выглядеть так:
import functools
from kata7 import vowel_count as vc
class Test:
solution = functools.partial(vc.count_vowels_bc1) # choose function to test
def test_all_vowels(self):
assert self.solution("aaaiiooooyyyuuu") == 12
def test_zero_vowels(self):
assert self.solution("bbc") == 0